常量表达式中的条件运算符

编程入门 行业动态 更新时间:2024-10-11 23:18:06
本文介绍了常量表达式中的条件运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

常量表达式中的条件运算符

本文发布于:2023-11-06 03:50:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562651.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常量   表达式   运算符   条件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!