Topic: Initializer lists in default arguments and range-based for loops
Author: SG <s.gesemann@gmail.com>
Date: Tue, 8 Sep 2009 12:45:55 CST Raw View
On 13 Aug., 19:48, litb wrote:
> It turns out that based on the draft n2914 initializer lists (the
> braced ones) cannot be used in function default arguments and range
> based for loops. In both cases, the problem is that an initializer
> list alone is not an expression in and of itself, and requires
> prefixing with a simple-type-specifier.
>
> The following two things wouldn't work
>
> // "{ 1, 2, 3 }" not an expression
> for(int i : { 1, 2, 3 }) ...;
>
> // "{ 1, 2, 3 }" not an assignment-expression
> void f(vector<int> v = { 1, 2, 3 });
Are you sure? 7.1.6.4/6 seems to disagree. It contains the following
examples:
auto x1 = {1,2}; // decltype(x1) is std::initializer_list<int>
auto x2 = {1,2.0}; // error: cannot deduce element type
Stroustrup's C++0x FAQ still contains the following example:
for (const auto x : { 1,2,3,5,8,13,21,34 }) cout << x << '\n';
(http://www.research.att.com/~bs/C++0xFAQ.html)
Cheers!
SG
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: litb <Schaub-Johannes@web.de>
Date: Thu, 13 Aug 2009 11:48:06 CST Raw View
It turns out that based on the draft n2914 initializer lists (the
braced ones) cannot be used in function default arguments and range
based for loops. In both cases, the problem is that an initializer
list alone is not an expression in and of itself, and requires
prefixing with a simple-type-specifier.
The following two things wouldn't work
// "{ 1, 2, 3 }" not an expression
for(int i : { 1, 2, 3 }) ...;
// "{ 1, 2, 3 }" not an assignment-expression
void f(vector<int> v = { 1, 2, 3 });
Instead, the really ugly following syntax is needed to make this work
for(int i : initializer_list<int>{1, 2, 3}) ...;
In both cases, it appears to be non-ambiguous to me to support using
the "initializer-clause" production, which could either be a braced-
init-list or an assignment-expression. This would then exclude the
comma operator as a toplevel-expression - but do you really think the
following is all that worth to support? I don't
// oops, no "begin" for argument "int" found.
for(int i : 1, 2, 3) ...;
The change to support the range based for loop, as far as i can see
follow. Instead of saying at 6.5.4
"where __range, __begin, and __end are variables defined for
exposition only, _RangeT is the type of the expression, and begin-
expr and end-expr are determined as follows:"
We need to say
"where __range, __begin, and __end are variables defined for
exposition only, _RangeT is the type of the expression '__range', and
begin-expr and end-expr are determined as follows:"
Along with changing "expression" in the grammar to "initializer-
clause".
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]