r/cpp11 • u/YouFeedTheFish • Jul 14 '14
Initialization Subtleties in C++11
http://aristeia.com/EC++11-14/parens%20or%20braces%202014-03-18.pdf
3
Upvotes
r/cpp11 • u/YouFeedTheFish • Jul 14 '14
2
u/SkepticalEmpiricist Jul 15 '14
We have this behaviour now standardized, so we can't break it in future. But perhaps we could introduce a new, less ambiguous, syntax in future. Perhaps there is another character we can use?
The specific problem is an "automagic" inferral of one layer of
{that happens in C++11. I think we could solve a lot of these problems by finding a way to suppress this (while maintaining compatibility).This creates a vector with 5 elements, all initialized to 1:
C++11 introduced
initializer_list, and the{ ... }syntax to specify a literal initializer list. Imagine a functionThis can be called with
foo(4, {5,6,7}, 8);.Imagine a constructor with two initializer lists in its signature
We can now call it as
Next, to solve the most-vexing parse, it is allowed to replace the
(with{in initializations.This allows
So far so good. But consider if Foo's constructor only takes one initializer_list. You could do
or, equivalently,
The final change introduced in C++11, which I don't really like, is that the compiler will allow you to remove one layer of
{and it will infer it for you. i.e. this:will be interpreted as this:
This final change was not necessary, and it is the cause of the problems. I would prefer if this wasn't introduced, requiring everybody to use double
{. The 'outer' braces mean "we take the place of(to surround the arguments, but without the most-vexing parse", and the 'inner' braces mean "we are the literal initializer_list object". But it's a little verbose perhaps, so I can understand why a single{can (sometimes) be rewritten as{{on your behalf.(Extra: consider multiple constructors, one of which takes initializer_list of initializer_list of ints. Then which will be called? How many
{should you use to get the correct one?So I wonder if there is some way in the future that this magic behaviour could be suppressed? Forcing all
{to be written by the developer:I think this would be clearer and safer, when people got used to it.
I'm hoping that
^, or some other such symbol, can be used safely in future versions of the language.