initialization syntax: a = { ...

S

Stefan Ram

To initialize an array or ::std::vector object, I have seen both

.... a = { 1, 2, 3 };

and also

.... a{ 1, 2, 3 };

Does the use of the equals sign »=« make any difference?

(This question is not restricted to arrays and
::std::vectors as the type of »a«, but only asks
about the curly braces, not parentheses.)
 
Ö

Öö Tiib

To initialize an array or ::std::vector object, I have seen both

... a = { 1, 2, 3 };

and also

... a{ 1, 2, 3 };

Does the use of the equals sign »=« make any difference?

I have impression that *no* *differences* *whatsoever*.

I do not have C++11 under hand now (only N3485) but 8.5 tells that
'brace-or-equal-initializer' can be among other things '= braced-init-list'
or 'braced-init-list' and both result with the initialized thing becoming
8.4.5 "list-initialized". Standard does not seemingly make any
differences. Both invoke either same constructor (like with std::vector) or
do aggregate-initialization (like with std::array).

For my taste the initialization semantics of C++11 are too loose so I
suspect that something of it has to be adjusted in next revision.
Lot of surprising initialization code will compile without much noise
until then.
 
S

Stefan Ram

Öö Tiib said:
For my taste the initialization semantics of C++11 are too loose so I
suspect that something of it has to be adjusted in next revision.
Lot of surprising initialization code will compile without much noise
until then.

Somewhat sad is that the new »uniform initialization syntax« cannot
be used together with the new »auto« semantics. (Well, it /can/, but
usually not with the effect that would help a beginner.)
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top