如何在C 11中输出 enum class
的值?在C 03中,它是这样的:
#include <iostream>
using namespace std;
enum A {
a = 1,
b = 69,
c= 666
};
int main () {
A a = A::c;
cout << a << endl;
}
在c 0x中,此代码无法编译
#include <iostream>
using namespace std;
enum class A {
a = 1,
b = 69,
c= 666
};
int main () {
A a = A::c;
cout << a << endl;
}
prog.cpp:13:11: error: cannot bind 'std::ostream' lvalue to 'std::basic_ostream<char>&&'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/ostream:579:5: error: initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char, _Traits = std::char_traits<char>, _Tp = A]'
编译在Ideone.com
5 回答
与未作用域的枚举不同,作用域枚举不可隐式转换为其整数值 . 您需要使用强制转换将其显式转换为整数:
您可能希望将逻辑封装到函数模板中:
用作:
可以使用与unscoped枚举相同的语法来获取第二个示例(即使用范围枚举的示例) . 此外,该解决方案是通用的,适用于所有作用域枚举,而不是为每个作用域枚举编写代码(如@ForEveR提供的answer所示) .
解决方案是编写一个通用
operator<<
函数,该函数适用于任何作用域枚举 . 该解决方案采用SFINAE来自std::enable_if,如下所示 .(我不允许发表评论 . )我建议对James McNellis已经很好的答案进行以下改进:
同
constexpr
:允许我使用枚举成员值作为编译时数组大小static_assert
is_enum
:到'ensure'编译时该函数做了某事 . 仅提供枚举,如建议的那样顺便说一句,我问自己:为什么我想在我的枚举成员中分配数字值时使用
enum class
?考虑转换努力 .也许我会像我在这里建议的那样回到普通的
enum
:How to use enums as flags in C++?根据@TobySpeight的建议,没有static_assert的另一种(更好)的味道:
写得更简单,