enumeration declaration
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
枚举是一种特殊的类型,其值被限制到一个明确命名的常数(“统计员”)。这些常数的值是一个整数类型的基础类型的枚举值.
原文:
An enumeration is a distinct type whose value is restricted to one of several explicitly named constants ("enumerators"). The values of the constants are values of an integral type known as the underlying type of the enumeration.
enum name : type attr { enumerator = constexpr , enumerator = constexpr , ... None }
|
(1) | ||||||||
enum class name : type attr { enumerator = constexpr , enumerator = constexpr , ... None }
|
(2) | (C++11 起) | |||||||
enum struct name : type attr { enumerator = constexpr , enumerator = constexpr , ... None }
|
(3) | (C++11 起) | |||||||
声明无作用域的枚举类型。每个enumerator变得容易在封闭的范围内,并且是隐式转换为整数类型,包括bool。如果没有明确规定,基本型是一个不可分割的类型,能够代表所有枚举值,不能大于int,除非一些constexpr计算为一个常数,它不适合在int
2-3) 原文:
declares an unscoped enumeration type. Each enumerator becomes accessible in the enclosing scope, and is implicitly-convertible to integral type, including bool. If not explicitly specified, the underlying type is an integral type capable of representing all enumerator values, which cannot be wider than int unless some constexpr evaluates to a constant that does not fit in an int
声明一个范围的枚举类型。每个enumerator只能访问name::enumerator。转换为整型static_cast是可能的。如果没有明确规定,基本型是int.
原文:
declares a scoped enumeration type. Each enumerator can only be accessed as name::enumerator. Conversion to integral types is possible with static_cast. If not explicitly specified, the underlying type is int.
[编辑] 解释
name | - | 声明本声明的类型的名称。无作用域的枚举可能是一种莫名其妙,丧失理智的,在这种情况下,它不仅介绍了枚举名作为常量,但没有新的类型
原文: the name of the type declared by this declaration. An unscoped enumeration may be nameless, in which case it only introduces enumerator names as constants, but no new type |
type(C++11) | - | 可选的整数类型(任何CV资格被忽略),作为基础类型的枚举.
原文: optional integral type (any cv-qualification is ignored), used as the underlying type of the enumeration. |
attr(C++11) | - | 零个或多个实施特定的属性的形式
[[attribute]] 原文: zero or more implementation-specific attributes of the form [[attribute]] |
enumerator | - | 零个或多个枚举本声明。统计员的名称可以在任何地方使用,预计常量
原文: zero or more enumerators which are introduced by this declaration. The names of the enumerators may be used anywhere constants are expected |
constexpr | - | 可选的常量表达式的计算结果被分配到枚举的值。如果它被省略,该值是前一个枚举值的加1。如果省略该参数,该值是第一个枚举0
原文: optional constant expression which evaluates to the value to be assigned to the enumerator. If it is omitted, the value is the value of the previous enumerator plus 1. If omitted for the first enumerator, the value is 0 |
本章尚未完成 |
[编辑] 示例
#include <iostream> // color may be red (value 0), yellow (value 1), green (value 20), or blue (value 21) enum color { red, yellow, green = 20, blue }; // altitude may be altitude::high or altitude::low enum class altitude : char { high='h', low='l', // C++11 allows the extra comma }; // the constant d is 0, the constant e is 1, the constant f is 3 enum { d, e, f=e+2 }; int main() { color col = red; altitude a; a = altitude::low; std::cout << "red = " << col << " blue = " << blue << '\n' << "a = " << static_cast<char>(a) << '\n' << "f = " << f << '\n'; }
输出:
red = 0 blue = 21 a = l f = 3