gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 18:11:33 32 4
gpt4 key购买 nike

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

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com