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

c++11 std::array init

90 views
Skip to first unread message

Chris Forone

unread,
May 16, 2013, 3:02:43 AM5/16/13
to
hello group,

why can i initialize a local std::array in this way:

std::array<float, 3> locvar{1.0f, 2.0f, 3.0f};

but as member "only" in this way:

std::array<float, 3> member{{1.0f, 2.0f, 3.0f}};

i use mingw g++ 4.8.0 (32 bit).

thanks a lot, chris

Sam

unread,
May 16, 2013, 6:49:54 AM5/16/13
to
Chris Forone writes:

> hello group,
>
> why can i initialize a local std::array in this way:
>
> std::array<float, 3> locvar{1.0f, 2.0f, 3.0f};

Because you can't. That's the short answer.

> but as member "only" in this way:
>
> std::array<float, 3> member{{1.0f, 2.0f, 3.0f}};

These are two different things. The first one would be a constructor with
variadic parameters. The second one is a std::initializer_list. It shouldn't
be surpising that something called a "std::initializer_list" would be used
to initialize something.

I suppose that there is no technical reason why a variadic list of
parameters cannot be used to initialize an array or a vector, but that's
just the way it is.

Chris Forone

unread,
May 16, 2013, 7:19:10 AM5/16/13
to
Am 16.05.2013 12:49, schrieb Sam:
> Chris Forone writes:
>
>> hello group,
>>
>> why can i initialize a local std::array in this way:
>>
>> std::array<float, 3> locvar{1.0f, 2.0f, 3.0f};
>
> Because you can't. That's the short answer.
>
but i do and it compiles fine!

>> but as member "only" in this way:
>>
>> std::array<float, 3> member{{1.0f, 2.0f, 3.0f}};
>
> These are two different things. The first one would be a constructor
> with variadic parameters. The second one is a std::initializer_list. It
> shouldn't be surpising that something called a "std::initializer_list"
> would be used to initialize something.
>
> I suppose that there is no technical reason why a variadic list of
> parameters cannot be used to initialize an array or a vector, but that's
> just the way it is.
>
can a ctor have both kinds of braces? () {}
im not surprised about the function of the initializer_list and dont
understand it, if there is some kind of (sarcastic?) joke in there...

Chris Forone

unread,
May 21, 2013, 3:33:13 AM5/21/13
to
Am 16.05.2013 12:49, schrieb Sam:
ok, i think i understand now: the one is brace elision with copy-ctor,
the other is direct aggregate-initialization of the c-style array in
std::array<>

cheers, chris
0 new messages