为什么不能在三元运算符中使用braced-init-list? [英] Why can braced-init-list not be used in ternary operator?

查看:82
本文介绍了为什么不能在三元运算符中使用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-init-list?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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