std ::更少的枚举 [英] std::less on enums

查看:89
本文介绍了std ::更少的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准是否保证std::less<MyEnumType>将对MyEnumType进行排序,就像将MyEnumType的值强制转换为适当大小的整数类型一样?

Does the standard guarantee that std::less<MyEnumType> will order MyEnumType as if a value of MyEnumType was cast to an appropriately sized integer type?

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

推荐答案

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

Yes, std::less::operator() is defined as (§20.8.5/5):

operator()返回x < y

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

For using relational operators on enumeration types, the following is stated (§5.9/2):

通常的算术转换是对算术或枚举类型的操作数执行的.

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

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

For unscoped enumeration types, the usual arithmetic conversions are defined as doing integral promotion. Integral promotion for unscoped enumeration types is defined as (§5/9):

其基础类型不固定(7.2)的无范围枚举类型的prvalue可以转换为可以表示该枚举的所有值的以下类型的第一个prvalue,即范围b中的值 min 到b max ,如7.2中所述):intunsigned intlong intunsigned long intlong long intunsigned long long int. /p>

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.

如果有需要,将使用扩展的整数类型.

An extended integer type will be used if available and required.

这篇关于std ::更少的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆