标准是否保证std::less<MyEnumType>
会订MyEnumType
好像值为 MyEnumType
被转换为适当大小的整数类型?
enum MyEnumType { E1 = 0, E2 = 6, E3 = 3 };
最佳答案
是的,std::less::operator()
定义为 (§20.8.5/5):
operator()
returnsx < 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
, orunsigned long long int
.
如果可用且需要,将使用扩展整数类型。
关于c++ - std::less 枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13750296/