常量表达式中的条件运算符 [英] Conditional-Operator in Constant Expression

查看:132
本文介绍了常量表达式中的条件运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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.

这篇关于常量表达式中的条件运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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