Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "dereference a type" in C++03?

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.)

like image 861
user541686 Avatar asked Sep 04 '11 20:09

user541686


2 Answers

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; }
};
like image 62
fredoverflow Avatar answered Sep 28 '22 15:09

fredoverflow


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.

like image 44
Puppy Avatar answered Sep 28 '22 16:09

Puppy