How do I get the "dereferenced type" of another type in C++03? Note that it can be other dereferenceable type like std::vector<int>::iterator
.
e.g. if I have
template<typename T>
struct MyPointer
{
T p;
??? operator *() { return *p; }
};
How can I figure out what to replace the ???
with?
(No Boost! I want to know how to figure it out myself.)
template<typename>
struct dereference;
template<typename T>
struct dereference<T*>
{
typedef typename T type;
};
template<typename T>
struct MyPointer
{
T p;
typename dereference<T>::type operator *() { return *p; }
};
In the general case, you can't. For raw pointers, you can partially specialize as shown in other answers- custom smart pointers may have a common typedef for the result type. However, you cannot write a single function that will cope with any pointer in C++03.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With