码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-07-08 20:16:07      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:function   name   scope   cap   fun   like   log   cout   ast   

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的值

标签:function   name   scope   cap   fun   like   log   cout   ast   

原文地址:https://www.cnblogs.com/tuhooo/p/9280956.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!