c++ - 如何通过创建类型特征对 NTTP 类进行分类?
问题描述
在 C++20 中,NTTP 扩展了新类型,为我们带来了术语结构:
- l 值参考
- 不可缺少的
- 指针、成员指针和空指针
- 枚举
- 浮点
- 一个类,其中:所有基类和非静态数据成员都是公共的和不可变的,并且数据成员和基类的类型也必须是递归结构的(我猜)以及数组。
来自:https ://en.cppreference.com/w/cpp/language/template_parameters
这是一个解决方案,它不会简单地工作:
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;
我不完全确定这是否有效,但我担心默认初始化(我也不能使用std::declval
)。
如果不可能实现,它是否涉及编译器魔法?
解决方案
template <auto>
struct nttp_test {};
template<class T>
concept structural = requires { []<T x>(nttp_test<x>) { }; };