To be more specific, suppose I am writing template<class Pointer> class Foo
and I want to declare a typedef
inside the class for the type that *p
would have if p
were of type Pointer
.
In C++03, as far as I am aware, the only way to do this is with something like
typename std::iterator_traits<Pointer>::reference
The disadvantage of this method is that it won't work if Pointer
is some custom iterator type and the author forgot to extend std::iterator
or otherwise define a std::iterator_traits
specialization.
In C++11, my colleague suggested
decltype(*Pointer())
But this won't work if Pointer
is not default-constructible, so he amended this to
decltype(**(Pointer*)0)
I tried this, and it worked, but then I thought that it looked a bit iffy because it involves the dereference of a null pointer, and thus might not be standards-compliant.
Can we do better?