首页 > 解决方案 > 如何通过创建类型特征对 NTTP 类进行分类?

问题描述

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

来自: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)。

如果不可能实现,它是否涉及编译器魔法

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

解决方案


template <auto>
struct nttp_test {};

template<class T> 
concept structural = requires { []<T x>(nttp_test<x>) { }; };

演示。


推荐阅读