小编nok*_*okz的帖子

是否可以通过 C++20 中的非类型模板参数使用特征来确定对象是否属于类类型?

考虑这段代码:

#include <type_traits>

template <typename T>
struct wrapper {
    T& ref;
    constexpr wrapper(T& ref) : ref(ref) {}
};

// Trait that checks whether a type is of the form `wrapper<T>`
template <typename T>
struct is_wrapper_type : std::false_type {};

template <typename T>
struct is_wrapper_type<wrapper<T>> : std::true_type {};

// Trait that checks whether an object is of the type `wrapper<T>`
template <auto& Value>
struct is_wrapper_object;

template <auto& Value>
    requires (!is_wrapper_type<std::decay_t<decltype(Value)>>::value)
struct is_wrapper_object<Value> : std::false_type {};

template <auto& Value>
    requires is_wrapper_type<std::decay_t<decltype(Value)>>::value
struct is_wrapper_object<Value> …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer compiler-bug c++20 non-type-template-parameter

8
推荐指数
1
解决办法
228
查看次数