我如何定义一个模板类,它提供一个整数常量,表示作为输入模板参数提供的(指针)类型的“深度”?例如,如果类名为 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/