为什么一个braced-init-list不是一个表达式? [英] Why is a braced-init-list not an expression?

查看:303
本文介绍了为什么一个braced-init-list不是一个表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我正在阅读C ++ 11标准的第93页$ 5.1.2时,在这种情况下,它表示在这种情况下使用braced-init-list是非常的:
auto x = [] { return {1,2}}; //错误:一个braced-init-list不是表达式



我发现了这两个主题,一个来自标准,另一个来自N3681提案。 p>

$ 14.8.2.5:一个初始化器列表参数使参数被认为是非推断的上下文。
和$ 7.6.1.4:用新的发明类型模板参数U替换auto的出现,或者如果初始化器是一个带宽的初始化列表(8.5.4),则使用std :: initializer_list。



虽然N3691提案建议将大括号初始化的自动更改为不推导到初始化器列表,并且在支持初始化器具有超过一个元素,它说返回支持列表将不起作用,因为它不是一个表达
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3681.html



但是,我没有找到为什么是一个braced-init-list不是一个表达式?它可能具有这个主题相同的含义:
为什么我们不能自动推出返回类型?
但那里有一点区别,他试图理解为什么C ++ commitee结束这种语法是毫无价值的。所以一定有一个特别的理由,非常感谢你。

解决方案

http://www.stroustrup.com/default-argument.pdf


初始化程序列表不是表达式的原因就是
我们决定(正确地说,IMO)不允许在左边的
手边的初始化器列表的作业,作为++的操作数等,并进一步决定
(再次正确地,IMO)通过语法来执行。



While I am reading page93 $5.1.2 of the C++11 standard, during which it said it is ellegal for you to use the braced-init-list in this case: auto x=[]{return {1,2}}; //error: a braced-init-list is not an expression

And I have found these two topics, one from the standard and the other from N3681 proposal.

Page397 $14.8.2.5:an initializer list argument causes the parameter to be considered a non-deduced context. and $7.6.1.4:replacing the occurrences of auto with either a new invented type template parameter U or, if the initializer is a braced-init-list (8.5.4), with std::initializer_list.

While the N3691 proposal suggested "to change a brace-initialized auto to not deduce to an initializer list, and to ban brace-initialized auto for cases where the braced-initializer has more than one element. " and it said "returning a braced-list won't work as it's not an expression" http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3681.html

However, I failed to find "Why is a braced-init-list not an expression?" It may have the same meaning of this topic: Why can't we have automatically deduced return types? but there's a little differences while he was trying to understand why the C++ commitee concluded this kind of grammar was worthless. So there must be a particular reason for this?Thank you very much.

解决方案

Quoting from http://www.stroustrup.com/default-argument.pdf:

The reason that an initializer list isn’t an expression is simply that we decided (correctly, IMO) not to allow initializer lists on the left hand side of assignments, as operands of ++, etc. and further decided (again correctly, IMO) to enforce that through the grammar.

这篇关于为什么一个braced-init-list不是一个表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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