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'd agree that this constructor in particular should have more naming info attached. In many other cases, I find constructors to be descriptive with no additional naming info taking up space.
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