如何编写类型trait判断类型是否符合C++20 NTTP的structural要求
在C++20中,非类型模板参数(NTTP)扩展了支持的新类型,由此引入了structural概念,符合要求的类型如下:
- 左值引用
- 整型
- 指针、成员指针以及空指针
- 枚举类型
- 浮点类型
- 满足以下条件的类:所有基类和非静态数据成员均为公开且不可修改,数据成员和基类的类型也需递归满足structural要求,数组同样适用该规则。
以下是一个无法直接正常工作的尝试实现:
template <auto> struct nttp_test {}; template <typename T, typename = void> struct is_structural : std::false_type {}; template <typename T> struct is_structural<T, std::void_t<nttp_test<T{}>>> : std::true_type {}; template <typename T> inline constexpr bool is_structural_v = is_structural<T>::value;
目前无法确定该实现是否有效,且对*default initialization(默认初始化)*存在顾虑,同时也无法使用std::declval。如果该需求无法实现,是否需要依赖编译器内置能力?
内容的提问来源于stack exchange,提问作者Desmond Gold
相关产品推荐
相关产品推荐

