如何递归解除对指针的引用(C ++ 03)? [英] How to recursively dereference pointer (C++03)?

查看:107
本文介绍了如何递归解除对指针的引用(C ++ 03)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试递归地取消引用C ++中的指针.

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.

我有此代码:

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?

我看到我的问题已被标记为使用Boost的类似问题的重复;但是,我需要一个没有Boost (或任何其他库)的解决方案.

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

示例:

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

推荐答案

在MSVC-2008上完全没有依赖项,很简单.

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

这篇关于如何递归解除对指针的引用(C ++ 03)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆