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

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


In C++20, NTTP extends new types which brings us the term structural:

From: https://en.cppreference.com/w/cpp/language/template_parameters

Here is a work around implementation that won't simply work:

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;

I'm not entirely sure if that works but I'm worried about the default initialization (I can't also use std::declval).

If it's impossible to implement, does it involve compiler magic?


Solution

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

    Demo.