如何通过创建类型特征来 class 化 NTTP class?

How to classify an NTTP class by creating a type trait?

在 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++20 结构可能对这里有帮助:

template<auto> class Meow {};

template<class T> concept meow = requires { typename Meow<T{}>; };

struct Nyan {};

int main() {
        std::cout << std::boolalpha << meow<int> << '\n';
        std::cout << std::boolalpha << meow<Nyan> << '\n';
        std::cout << std::boolalpha << meow<std::string> << '\n';
}

要将概念转化为良好的旧特征,嗯... template<class T> using is_meow = std::bool_constant<meow<T>>;

template <auto>
struct nttp_test {};

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

Demo.