我在MSVC 10上尝试了以下代码片段,在该代码片段中可以正常工作。
I tried the following code snippet with MSVC 10, where it works fine.
enum { FOO = (sizeof(void*) == 8 ? 10 : 20) }; int main() { return FOO; }我想知道的是:C ++标准(最好是C + +98)允许我在所有操作数都是常量表达式的情况下在常量表达式中使用条件运算符,或者这是Microsoft的古怪/扩展吗?
What I would like to know is: Does the C++ Standard (preferably C++98) allow me to use the conditional-operator in a constant expression when all operands are constant expressions, or is this a Microsoft quirk/extension?
推荐答案这是完全有效和明智的标准C ++。
This is perfectly valid and sensible standard C++.
三元条件运算符形成一个 expression ,该表达式是一个
The ternary conditional operator forms an expression, and the expression is a constant expression if its operands are.
标准引用为C ++ 11 5.19 / 2:
The standard reference is C++11 5.19/2:
条件表达式是核心常量表达式 [...]
请注意,从5.16开始,三元条件表达式是 conditional-expressions 的一种。其他类型例如 2 == 3 。
Note that by 5.16, ternary conditional expressions are one type of conditional-expressions. Other types are things like 2 == 3.
更多推荐
常量表达式中的条件运算符
发布评论