How to “dereference a type” in C++03?

2019-06-22 00:10发布

问题:

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

回答1:

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.



回答2:

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; }
};


回答3:

You can have a simple construct which recursively removes all the pointers from a given type as below:

template<typename T>
struct ActualType { typedef T type; };
template<typename T>
struct ActualType<T*> { typedef typename ActualType<T>::type type; };

Below is the inline wrapper function to recursively find out the actual value from a given pointer or non-pointer types;

template<typename T>
typename ActualType<T>::type ActualValue (const T &obj) { return obj; }
template<typename T>
typename ActualType<T>::type ActualValue (T *p) { return ActualValue(*p); }

And just use it as:

template<typename T>
struct MyPointer
{
  T p;
  typename ActualType<T>::type operator *() { return ActualValue(p); }
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^                       ^^^^^^^^^^^^^^
};

In this example, it removes all the pointers from a given type, but as per the need you can configure the ActualType<> and ActualValue<>. There won't be any compiler error even if you declare MyPointer<> with a non-pointer type.

Here is a working demo for a single pointer and no pointer types.



回答4:

You can do it like this, and it is ensured that the template will only compile when you pass pointers to it:

template<typename T>
struct MyPointer<T*>
{
    T* p;
    T operator*() { return *p; }
}