gpt4 book ai didi

c++ - std::less 枚举

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:16:05 25 4
gpt4 key购买 nike

标准是否保证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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com