1. 程式人生 > >C++11中如何輸出enum class的值

C++11中如何輸出enum class的值

gic log its may aps cti return cout cast

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<std::underlying_type<A>::type>(a) << std::endl;
You may want to encapsulate the logic into a function template:

template <typename Enumeration>
auto as_integer(Enumeration const value)
-> typename std::underlying_type<Enumeration>::type
{
return static_cast<typename std::underlying_type<Enumeration>::type>(value);
}
used as:

std::cout << as_integer(a) << std::endl;

C++11中如何輸出enum class的值