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)
Why would you want to use braces to call other constructors in the first place? The only reason to write initializater_list constructors in the first place is usually because you want your type to behave like an array / container. If that is the case, then why would you expect my_array a{1,2} to do anything but fill a with the values 1,2?
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