如何定义提供指针深度/类型级别的模板类? [英] How can I define a template class which gives the pointer depth/level of a type?

查看:117
本文介绍了如何定义提供指针深度/类型级别的模板类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何定义一个模板类,该类提供一个整数常量,该常量表示作为输入模板参数提供的(指针)类型的深度"?例如,如果该类名为Depth,则将满足以下条件:

How can I define a template class which provides an integer constant representing the "depth" of a (pointer) type provided as the input template argument? For example, if the class was called Depth, the following would be true:

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

这篇关于如何定义提供指针深度/类型级别的模板类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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