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.)
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.
You can do it like this, and it is ensured that the template will only compile when you pass pointers to it:
You can have a simple construct which recursively removes all the pointers from a given type as below:
Below is the inline wrapper function to recursively find out the actual value from a given pointer or non-pointer types;
And just use it as:
In this example, it removes all the pointers from a given type, but as per the need you can configure the
ActualType<>
andActualValue<>
. There won't be any compiler error even if you declareMyPointer<>
with a non-pointer type.Here is a working demo for a single pointer and no pointer types.