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

…衆ロ難τιáo~ 提交于 2020-01-01 02:34:25

问题


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:


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


来源:https://stackoverflow.com/questions/8022821/how-to-recursively-dereference-pointer-c03

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!