Created
February 6, 2015 06:56
-
-
Save nbdd0121/b5a70d0467b244bb40e4 to your computer and use it in GitHub Desktop.
Test on degree of support of decltype on various compilers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
template<typename T> | |
class Pointer{ | |
private: | |
T* ptr_; | |
public: | |
Pointer(T* ptr): ptr_(ptr) {} | |
operator T*() const { | |
return ptr_; | |
} | |
T& operator *() const { | |
return *ptr_; | |
} | |
T* operator ->() const { | |
return ptr_; | |
} | |
const Pointer& operator =(const Pointer& ptr) { | |
ptr_ = ptr.ptr_; | |
return *this; | |
} | |
const Pointer& operator =(T* ptr) { | |
ptr = ptr; | |
return *this; | |
} | |
template<typename Arg> | |
auto operator[](Arg index) -> decltype(this->operator*()[index]) { // GCC Passed, clang Passed, MSVC Failed | |
return operator*()[index]; | |
} | |
// template<typename Arg> | |
// auto operator[](Arg index) -> decltype(operator*()[index]) { // GCC Failed, clang Passed, MSVC Passed | |
// return operator*()[index]; | |
// } | |
// template<typename Arg> | |
// auto operator[](Arg index) -> decltype((**this)[index]) { // GCC Passed, clang Passed, MSVC Failed | |
// return operator*()[index]; | |
// } | |
}; | |
int main(){ | |
int array[4] {1 ,2}; | |
int* arrayPtr = array; | |
Pointer<int*> ptr(&arrayPtr); | |
std::cout << ptr[1]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment