Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

See if this usage about initializer_list is conforming to the final draft

789 views
Skip to first unread message

hayate

unread,
Jun 9, 2011, 9:02:48 AM6/9/11
to

code:
http://ideone.com/T6GFO

#include <array>
#include <initializer_list>

using namespace std;

class A {
public:
A(array<int, 4>) {};
};

int main() {
A a({1,2,3,4});
}

Its compilation fails on GCC 4.6
I checked n3291 at 8.5.4/1, it seems here for constructing a is a
direct-initialization, thus array's aggregation can be invoked.
Am I right?


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Daniel Krügler

unread,
Jun 9, 2011, 5:22:24 PM6/9/11
to

Am 09.06.2011 15:02, schrieb hayate:
>
> code:
> http://ideone.com/T6GFO
>
> #include<array>
> #include<initializer_list>
>
> using namespace std;
>
> class A {
> public:
> A(array<int, 4>) {};
> };
>
> int main() {
> A a({1,2,3,4});
> }
>
> Its compilation fails on GCC 4.6
> I checked n3291 at 8.5.4/1, it seems here for constructing a is a
> direct-initialization, thus array's aggregation can be invoked.
> Am I right?

You are right that std::array's aggregate initialization can be invoked,
but you are overlooking that (at least a non-zero-length) std::array
conceptually is an aggregate containing another aggregate (the actual C
array) as member. In this context the so-called brace-list elision
cannot be applied, you need to provide the correct number of {} levels,
which is in this case 2, thus

A a({{1,2,3,4}});

is necessary here. This is described in [over.ics.list] p4 of the FDIS:

"Otherwise, if the parameter has an aggregate type which can be
initialized from the initializer list according to the rules for
aggregate initialization (8.5.1),[..]"

HTH & Greetings from Bremen,

Daniel Krügler

Helmut Jarausch

unread,
Jun 9, 2011, 5:54:10 PM6/9/11
to

On Thu, 09 Jun 2011 07:02:48 -0600, hayate wrote:

> code:
> http://ideone.com/T6GFO
>
> #include <array>
> #include <initializer_list>
>
> using namespace std;
>
> class A {
> public:
> A(array<int, 4>) {};
> };
>
> int main() {
> A a({1,2,3,4});
> }
>
> Its compilation fails on GCC 4.6
> I checked n3291 at 8.5.4/1, it seems here for constructing a is a
> direct-initialization, thus array's aggregation can be invoked. Am I
> right?

I've been bitten by this myself. 'array' is an aggregate, there is a long
thread on comp.std.c++ why, thus you have to replace the declaration of
'a' by
A a({{1,2,3,4}}); // note the double braces

Helmut.


--
Helmut Jarausch
Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany

Daniel Krügler

unread,
Jun 10, 2011, 5:13:07 AM6/10/11
to
Am 09.06.2011 23:54, schrieb Helmut Jarausch:

[..]

> I've been bitten by this myself. 'array' is an aggregate, there is a long
> thread on comp.std.c++ why, thus you have to replace the declaration of
> 'a' by
> A a({{1,2,3,4}}); // note the double braces

This is a false herring: The problem is not because array is an
aggregate, but because array is an aggregate *containing* an aggregate.
This becomes obvious, if we look at a slightly different example:

struct aggr {
int i1, i2, i3, i4;
};

struct B {
B(aggr){}
};

int main() {
B({1,2,3,4}); // OK
}

In this case, we have no wrapped aggregate, therefore we need exactly
one level of braces.

HTH & Greetings from Bremen,

Daniel Krügler


--

0 new messages