This is the main problem i found with the new decidedly not uniform initialisation syntax - it adds yet another way to initialise stuff that seems to only be appropriate in some situations with all sorts of little quirks to it that make it not universally applicable
As far as I can tell, its basically good for initialising container types like std::vector and map, and that's about it
I'm not a c++ expert by any stretch though so maybe i'm totally missing something
There are reasons to prefer brace initialization in general. The only place I’m aware of that it’s not always possible is that a std::initializer_list constructor is always preferred, so you need parens if there is ambiguity, and this can be tricky in a template.
Braced initialization prevents narrowing conversions so you get some additional type safety. It also prevents the most vexing parse. Of course you also get nice collection literals as you mentioned.
FWIW the style I've converged on is "use brace initialization in almost all cases, and avoid ever writing `std::initializer_list` constructors"
the braced initialization can be really nice in templates because it can also initialize the aggregates correctly in cases where paren initialization would fail (because using the parentheses initialization cannot become aggregate initialization IIUC)
For a vector its pretty nice to have an initializer_list constructor i would argue, same for std::set or similar classes. Would you argue that even those perks are not worth the trouble?
I love being able to easily initialize containers with a list of elements, as well as other clever uses like a JSON object. However, I don't think making it the same syntax used to call other constructors was a good choice. It's hard to say whether a plain list constructor is worth the trouble to me or if I'd consider an alternate best-we've-got constructor like vector foo = list_of(1, 4, 5); or vector foo{from_list, 1, 4, 5};, where either would be uniform across classes that use it. If I could have my way, I'd probably prefer an alternate syntax. For example, vector foo{[1, 4, 5]};.
I'm not surprised. "My" syntax seems rather obbvious and I'm definitely not the first one who thought of it. I was just hoping someone would say what's the corner case here.
41
u/James20k P2005R0 Jun 06 '18
This is the main problem i found with the new decidedly not uniform initialisation syntax - it adds yet another way to initialise stuff that seems to only be appropriate in some situations with all sorts of little quirks to it that make it not universally applicable
As far as I can tell, its basically good for initialising container types like std::vector and map, and that's about it
I'm not a c++ expert by any stretch though so maybe i'm totally missing something