为什么不能在三元运算符中使用braced

编程入门 行业动态 更新时间:2024-10-26 10:32:26
本文介绍了为什么不能在三元运算符中使用braced-init-list?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的编译器是最新的VC ++ 2013 RC。

My compiler is the latest VC++ 2013 RC.

int f(bool b) { return {}; // OK return b ? 1 : { }; // C2059: syntax error : '{' return b ? 1 : {0}; // C2059: syntax error : '{' return b ? {1} : {0}; // C2059: syntax error : '{' }

为什么可以支撑初始化-列表不能在三元运算符中使用?

Why can braced-init-list not be used in ternary operator?

此行为是C ++标准定义的格式错误还是VC ++编译器的错误? strong>

Is this behavior defined as ill-formed by the C++ standard, or just a bug of the VC++ compiler?

推荐答案

好吧,这是标准关于braced-init-list(8.5.3.1)的内容:

Well, here's what the standard says about the braced-init-list (8.5.3.1):

可以使用列表初始化

List-initialization can be used

  • 作为变量定义中的初始化程序(8.5)
  • 作为新表达式中的初始化程序(5.3.4)
  • 在return语句中( 6.6.3)
  • 作为函数参数(5.2.2)
  • 作为下标(5.2.1)
  • 作为构造函数调用的参数(8.5,5.2.3)
  • 作为非静态数据成员的初始化程序(9.2)
  • 在内存初始化器(12.6.2)
  • 在任务右侧(5.17)
  • as the initializer in a variable definition (8.5)
  • as the initializer in a new expression (5.3.4)
  • in a return statement (6.6.3)
  • as a function argument (5.2.2)
  • as a subscript (5.2.1)
  • as an argument to a constructor invocation (8.5, 5.2.3)
  • as an initializer for a non-static data member (9.2)
  • in a mem-initializer (12.6.2)
  • on the right-hand side of an assignment (5.17)

由于这里没有提到条件运算符,所以我猜您的编译器是正确的。还要注意,条件运算符期望:(5.16)的两面都有表达式,据我所知,花括号初始化器不是表达式。

Since this doesn't mention the conditional operator, I guess your compiler is right. Also note that the conditional operator expects expressions on the both sides of : (5.16), and as far as I understand, a brace-initializer is not an expression.

更多推荐

为什么不能在三元运算符中使用braced

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

发布评论

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

>www.elefans.com

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