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

Uniform initialization vs list initialization

32 views
Skip to first unread message

Andre Zunino

unread,
Mar 5, 2016, 9:46:10 PM3/5/16
to
Hello.

Some years ago, when I was first experimenting with C++11's new
features, I remember feeling excited about the possibility of directly
initializing containers from a literal list of values. I also remember
being bit by the fact that compilers will always favor constructor
overloads that take std::initializer_list. I felt disappointed and
wondered how that could have been overlooked or accepted by the
committee. Why did they not choose a syntax which would not lead to
ambiguity? Wouldn't having braces around list elements (in addition to
the braces for the uniform initialization syntax) take care of it?

a) std::vector<int> v{ {4, 1} };
b) std::vector<int> w( {4, 1} };
c) std::vector<int> x{4, 1};
d) std::vector<int> y(4, 1);

a) and b) should produce vector objects with 2 elements, 4 and 1. c) and
d) should produce vectors with 4 elements, all of them equal to 1.

Here is a discussion on Stack Overflow:
http://stackoverflow.com/questions/22501368/why-wasnt-a-double-curly-braces-syntax-preferred-for-constructors-taking-a-std

And here's a post by Scott Meyers on different, but related issues:
http://scottmeyers.blogspot.com.br/2015/09/thoughts-on-vagaries-of-c-initialization.html

Does anybody know if there's any work being done to mend this situation?

Thank you.

Paavo Helde

unread,
Mar 6, 2016, 4:35:47 AM3/6/16
to
What I do not understand is the desire to express everything with the
same symbol. Of course you can encode everything by repeating the symbol
arbitrary number of times ("unary numeral system"), but this does not
make it a good idea.

Why couldn't the braces used uniformly for initializer lists (i.e. data
values) and parens for other kind of constructors?

a) std::vector<int> v{ {4, 1} }; // 2 elements or syntax error
b) std::vector<int> w( {4, 1} ); // 2 elements (calling suitable ctor)
c) std::vector<int> x{4, 1}; // 2 elements
d) std::vector<int> y(4, 1); // 4 elements

Fur curiosity, I ran this through the compilers and it appears that's
exactly how it works! It's nice to see C++11 actually matches my
intuition ;-) (I have not used initializer lists so far so should not be
biased).


Juha Nieminen

unread,
Mar 6, 2016, 6:04:15 PM3/6/16
to
Paavo Helde <myfir...@osa.pri.ee> wrote:
> What I do not understand is the desire to express everything with the
> same symbol.

I suppose that the purpose is to unify struct and class initialization.

This is valid in both C and C++:

struct S { int a, b; };
S s = S { 1, 2 };

The problem with that is, of course, that if in C++ you would now want
to replace that compiler-generated initialization in the struct with
your own constructor, it won't work in C++98. With the new unified
construction you can add a constructor to S taking two ints, and that
syntax will still work, and call the new constructor.

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---
0 new messages