How to recursively dereference pointer (C++03)?

2019-03-18 11:00发布

I'm trying to recursively dereference a pointer in C++.

If an object is passed that is not a pointer (this includes smart pointers), I just want to return the object itself, by reference if possible.

I have this code:

template<typename T> static T &dereference(T &v) { return v; }
template<typename T> static const T &dereference(const T &v) { return v; }
template<typename T> static T &dereference(T *v) { return dereference(*v); }

My code seems to work fine in most cases, but it breaks when given function pointers, because dereferencing a function pointer results in the same exact type of function pointer, causing a stack overflow.

So, how can I "stop" the dereferencing process when the dereferenced type has the same type as the original object?

Note:

I see my question has been marked as a duplicate of a similar question that uses Boost; however, I need a solution without Boost (or any other libraries).


Example:

template<typename T> T &dereference(T &v) { return v; }
template<typename T> const T &dereference(const T &v) { return v; }
template<typename T> T &dereference(T *v) { return dereference(*v); }

template<typename TCallback /* void(int) */>
void invoke(TCallback callback) { dereference(callback)(); }

void callback() { }

struct Callback {
     static void callback() { }
     void operator()() { }
};

int main() {
    Callback obj;
    invoke(Callback());          // Should work (and does)
    invoke(obj);                 // Should also work (and does)
    invoke(&obj);                // Should also work (and does)
    invoke(Callback::callback);  // Should also work (but doesn't)
    invoke(&Callback::callback); // Should also work (but doesn't)
    invoke(callback);            // Should also work (but doesn't)
    invoke(&callback);           // Should also work (but doesn't)
    return 0;
}

1条回答
淡お忘
2楼-- · 2019-03-18 11:51

No dependencies at all, simple, should work on MSVC-2008.

template<typename T>
struct is_function
{
    static char     check(...);
    static double   check(const volatile void*); // function pointers are not convertible to void*
    static T        from;
    enum { value = sizeof(check(from)) != sizeof(char) };
};

template<bool, typename T = void>
struct enable_if{};

template<typename T>
struct enable_if<true, T>{typedef T type;};

template<typename T> 
T& dereference(T &v){return v;}

template<typename T> 
const T& dereference(const T& v){return v;}

template<typename T> 
typename enable_if<!is_function<T>::value, T&>::type dereference(T* v){return dereference(*v);}
查看更多
登录 后发表回答