c++ - 如何通过创建类型特征来对 NTTP 类进行分类?

标签 c++ c++20 non-type-template-parameter

<分区>

在 C++20 中,NTTP 扩展了新类型,这给我们带来了术语结构:

  • 左值引用
  • 整体
  • 指针、指向成员的指针和空指针
  • 枚举
  • float
  • 一个类,其中:所有基类和非静态数据成员都是公共(public)的和不可变的,并且数据成员和基类的类型也必须结构化递归(我猜)并且数组。

发件人: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>) { }; };

Demo.

关于c++ - 如何通过创建类型特征来对 NTTP 类进行分类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68855731/

相关文章:

c++ - GCC 的 <experimental/ranges> 过滤器 View 无法使用无限范围 iota() 进行编译

c++ - C++20 模块实现分区的 hpp/cpp 拆分

c++ - 默认相等运算符是否有任何 C++20 功能测试?

c++ - 非类型模板参数和要求

c++ - QT C++中对全局变量的 undefined reference

java.lang.UnsatisfiedLinkError : No implementation found? 错误

c++ - 在 arm Debian 上加载共享库时出错

c++ - Qt raw 与 std::shared_ptr

C++ - 如何调用递归继承的模板化基类的模板化方法

java - Java泛型可以用值而不是类型参数化吗?