为什么作用域枚举允许使用 |使用先前分配的值进行初始化时的运算符?
Why do scoped enums allow use of | operator when initializing using previously assigned values?
我正在将无作用域枚举转换为有作用域枚举,运行 解决了一个难题。
Stroustrup,C++ 编程语言,第 4 版,第 8.4.1 节,范围为枚举 类 的文档未隐式转换为整数类型,并提供代码运算符 |
和 &
作为如何使用 static_cast
解决该问题的示例。
在先前定义的 enum
值上使用 |
运算符进行以下初始化不应该是非法的吗?
enum class FileCopy {
PreviousHDUs = 1,
CurrentHDU = 2,
FollowingHDUs = 4,
AllHDUs = PreviousHDUs | CurrentHDU | FollowingHDUs,
CurrentHeader = 8
};
int main()
{
std::cout << static_cast<int>( FileCopy::AllHDUs) << "\n";
}
我已经在 Wandbox 上使用 clang 和 gcc HEAD 以及 --pedantic-errors
测试了它,它编译并 returns 预期输出 7
。这并不是说它是合法的,只是它似乎被编译器接受了。
这是明确记录的行为吗?我一直无法以描述此行为的方式解析 documentation。
根据 C++17 标准(8.5.13 按位包含或运算符)
1 The usual arithmetic conversions (8.3) are performed; the result is
the bitwise inclusive OR function of its operands. The operator
applies only to integral or unscoped enumeration operands.
和(10.2 枚举声明)
- ... For a scoped enumeration type, the underlying type is int if it is not
explicitly specified. In both of these cases, the underlying
type is said to be fixed. Following the closing brace of an
enum-specifier, each enumerator has the type of its enumeration. If
the underlying type is fixed, the type of each enumerator prior to the
closing brace is the underlying type and the constant-expression in
the enumerator-definition shall be a converted constant expression of
the underlying type
所以这是明确记录的行为。
[dcl.enum]/5:
... If the underlying type is fixed, the type of each enumerator prior to the closing brace is the underlying type ...
也就是说,在遇到右大括号之前,每个枚举数的类型都是 int
。在那之后,枚举器的类型为 FileCopy
,您将无法再像这样将它们按位或运算在一起。
我正在将无作用域枚举转换为有作用域枚举,运行 解决了一个难题。
Stroustrup,C++ 编程语言,第 4 版,第 8.4.1 节,范围为枚举 类 的文档未隐式转换为整数类型,并提供代码运算符 |
和 &
作为如何使用 static_cast
解决该问题的示例。
在先前定义的 enum
值上使用 |
运算符进行以下初始化不应该是非法的吗?
enum class FileCopy {
PreviousHDUs = 1,
CurrentHDU = 2,
FollowingHDUs = 4,
AllHDUs = PreviousHDUs | CurrentHDU | FollowingHDUs,
CurrentHeader = 8
};
int main()
{
std::cout << static_cast<int>( FileCopy::AllHDUs) << "\n";
}
我已经在 Wandbox 上使用 clang 和 gcc HEAD 以及 --pedantic-errors
测试了它,它编译并 returns 预期输出 7
。这并不是说它是合法的,只是它似乎被编译器接受了。
这是明确记录的行为吗?我一直无法以描述此行为的方式解析 documentation。
根据 C++17 标准(8.5.13 按位包含或运算符)
1 The usual arithmetic conversions (8.3) are performed; the result is the bitwise inclusive OR function of its operands. The operator applies only to integral or unscoped enumeration operands.
和(10.2 枚举声明)
- ... For a scoped enumeration type, the underlying type is int if it is not explicitly specified. In both of these cases, the underlying type is said to be fixed. Following the closing brace of an enum-specifier, each enumerator has the type of its enumeration. If the underlying type is fixed, the type of each enumerator prior to the closing brace is the underlying type and the constant-expression in the enumerator-definition shall be a converted constant expression of the underlying type
所以这是明确记录的行为。
[dcl.enum]/5:
... If the underlying type is fixed, the type of each enumerator prior to the closing brace is the underlying type ...
也就是说,在遇到右大括号之前,每个枚举数的类型都是 int
。在那之后,枚举器的类型为 FileCopy
,您将无法再像这样将它们按位或运算在一起。