Is it possible to somehow check whether a type is an enumeration, i.e. enum or enum class ?

  • Strange, two users created today, one asked a question - the second quickly answered, faster than anyone, and got enough points quickly. Something is not clean here - nick_n_a
  • @nick_n_a will have to accept another answer, otherwise they will be banned - Misha
  • @nick_n_a everything is fine, users are different, do not ban) - Nick Volynkin

2 answers 2

 #include <iostream> #include <type_traits> class A {}; enum E {}; enum class Ec : int {}; int main() { std::cout << std::boolalpha; std::cout << std::is_enum<A>::value << '\n'; std::cout << std::is_enum<E>::value << '\n'; std::cout << std::is_enum<Ec>::value << '\n'; std::cout << std::is_enum<int>::value << '\n'; } 

So or

 std::cout << std::is_enum<A>() << '\n'; std::cout << std::is_enum<E>() << '\n'; std::cout << std::is_enum<Ec>() << '\n'; std::cout << std::is_enum<int>() << '\n'; 

    Starting with C ++ 11 - yes. This can be done using the template class std::is_enum in the std::is_enum header file.