c++ - 我如何定义一个模板类来给出类型的指针深度/级别?

标签 c++ templates pointers types

我如何定义一个模板类,它提供一个整数常量,表示作为输入模板参数提供的(指针)类型的“深度”?例如,如果类名为 Depth,则以下内容为真:

Depth<int ***>::value == 3
Depth<int>::value == 0

最佳答案

template <typename T> 
struct pointer_depth_impl
{
    enum { value = 0 };
};

template <typename T>
struct pointer_depth_impl<T* const volatile>
{
    enum { value = pointer_depth_impl<T const volatile>::value + 1 };
};

template <typename T>
struct pointer_depth
{
    enum { value = pointer_depth_impl<T const volatile>::value };
};

关于c++ - 我如何定义一个模板类来给出类型的指针深度/级别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5520429/

相关文章:

c++ - 混合 Boost FOREACH 宏和 OpenMP 并行化

c++ - 从静态函数调用函数

在 typedef 中使用指针调用变量

c++ - 在 C++ 中初始化字符串

Windows 7 中具有非阻塞接收的 C++ TCP 套接字

C++-14 使用 enable_if_t 选择整数类型模板化类的成员函数

c++ - typeof(x) 作为模板参数?

c++ - MSVC 放弃带有 "fatal error C1060: compiler is out of heap space"的模板重代码

c++ - 链表内存读取错误

c++ - 在数组中获取和存储内存地址的问题