Reputation: 1123
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
?
Upvotes: 1
Views: 1609
Reputation: 96043
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 {};
Upvotes: 6
Reputation: 20918
You could use conditional_t
to select true_type
/false_type
as base class depending on the condition is true or not:
template<class E>
constexpr auto EnumToInt(E e) {
return static_cast<std::underlying_type_t<E>>(e);
}
enum class Foo {
None = 0
};
enum class Bar {
None = 1
};
template<class T>
struct NoneAs0 : std::conditional_t< EnumToInt(T::None) == 0,
std::true_type, std::false_type>
{};
int main() {
std::cout << NoneAs0<Foo>::value << std::endl; // 1
std::cout << NoneAs0<Bar>::value << std::endl; // 0
Upvotes: 3