c++11 std::array init

C

Chris Forone

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
 
C

Chris Forone

Am 16.05.2013 12:49, schrieb Sam:
Because you can't. That's the short answer.
but i do and it compiles fine!
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...
 
C

Chris Forone

Am 16.05.2013 12:49, schrieb Sam:
Because you can't. That's the short answer.


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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top