Reputation: 51
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.
Upvotes: 4
Views: 337
Reputation: 628
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.
Upvotes: 2