How can I output the value of an enum class in C++11? In C++03 it\'s like this:
#include
using namespace std;
enum A {
a =
Unlike an unscoped enumeration, a scoped enumeration is not implicitly convertible to its integer value. You need to explicitly convert it to an integer using a cast:
std::cout << static_cast::type>(a) << std::endl;
You may want to encapsulate the logic into a function template:
template
auto as_integer(Enumeration const value)
-> typename std::underlying_type::type
{
return static_cast::type>(value);
}
used as:
std::cout << as_integer(a) << std::endl;