c++ - std::less 枚举

标签 c++ enums language-lawyer

标准是否保证std::less<MyEnumType>会订MyEnumType好像值为 MyEnumType被转换为适当大小的整数类型?

enum MyEnumType { E1 = 0, E2 = 6, E3 = 3 };

最佳答案

是的,std::less::operator()定义为 (§20.8.5/5):

operator() returns x < y

对于在枚举类型上使用关系运算符,声明如下 (§5.9/2):

The usual arithmetic conversions are performed on operands of arithmetic or enumeration type.

对于无作用域的枚举类型,通常的算术转换 被定义为进行整数提升。无范围枚举类型的整体提升定义为 (§5/9):

A prvalue of an unscoped enumeration type whose underlying type is not fixed (7.2) can be converted to a prvalue of the first of the following types that can represent all the values of the enumeration (i.e., the values in the range bmin to bmax as described in 7.2): int, unsigned int, long int, unsigned long int, long long int, or unsigned long long int.

如果可用且需要,将使用扩展整数类型。

关于c++ - std::less 枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13750296/

相关文章:

c++ - OpenGL 的范围问题

java - 参数化枚举传入另一个枚举

java - 如何在三元运算符中使用枚举检查两个条件?

c++ - 具有依赖类型的 c++11 可变参数函数模板重载是否含糊不清?

c++ - 非多态类型上的 C 风格转换

c++ - 在不重复代码的情况下使用多个参数

c++ - Qdialog执行的返回值

c++ - CreateDepthStencilView() 失败

c# - 可以根据字符串名称加载枚举吗?

c++ - 可变参数模板必须具有非空参数包的可调用特化才能格式正确吗?