Search code examples
c++c++17type-traits

Type trait for enum member value


I have a type trait to check whether or not the enum class contains a member called None.

template<typename T, typename = void>
    struct has_none : std::false_type
    {
    };

template<typename T>
struct has_none<T,
    std::void_t<decltype(T::None)>> : std::true_type {};

This check will be coupled with the std::is_enum_v. The question is, how would I create a type_trait which would check that the Enum::None has value of 0? Is it even possible when talking about type_traits?


Solution

  • Use std::enable_if:

    template <typename T, typename = void>
    struct has_none : std::false_type {};
    
    template <typename T>
    struct has_none<T, std::enable_if_t<T::None == T(0)>> : std::true_type {};