Why is a braced-init-list not an expression?
Asked Answered
T

1

4

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.

Toilworn answered 2/8, 2013 at 5:52 Comment(0)
N
2

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.

Nimbus answered 15/8, 2013 at 20:2 Comment(1)
Fair enough, but it doesn't answer the question IMO. The first part ("not allowed on LHS") applies to e.g. &var (which is an expression in the grammar) and the second part says basically "it's not an expression (in the grammar) because we decided to not make it an expression in the grammar as a means of enforcing the first part" (which can be enforced other ways).Trash

© 2022 - 2024 — McMap. All rights reserved.