r/cpp • • Jun 05 '18

Initialization in Modern C++

https://twitter.com/timur_audio/status/1004017362381795329
379 Upvotes

51 comments sorted by

View all comments

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

27

u/SoundOfOneHand Jun 06 '18

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.

11

u/evaned Jun 06 '18 edited Jun 06 '18

Braced initialization prevents narrowing conversions so you get some additional type safety.

One of my coworkers who is on the committee pointed out these examples to me the other day.

I'm not sure if everyone would agree that's type safety exactly or agree with my valuation of these, but I'd call it such. And if I'm going to pick a rule of thumb, I think I'd rather have those examples fail to compile and allow narrowing conversions (as you get with parens) than the other way around (braces).

(He did say this is an area the committee is working on improving, though what that means I don't know. I suggested pulling from Unicode's reservoir of delimiters, perhaps S s«1, 2»;; that should fix Everything.)

10

u/tcanens Jun 06 '18

EWG just voted on Monday to not treat such types as aggregates.

Even without the change, I wouldn't find the examples persuasive. It's only triggered in corner cases (all-public data members and all constructors are either deleted or defaulted) that are highly unlikely to be present in anything other than intentionally constructed "WTF" snippets.

2

u/render787 Jun 06 '18

Can you explain this in more detail? Why would those types not be aggregates, what distinction did they vote in?

2

u/timur_audio C++ committee Jun 11 '18

@tcanens it's not true that they are just "WTF" snippets, we have found such types in real code, with an average frequency of one such type per 3 MLoC. And we broke them :)

1

u/tcanens Jun 11 '18

I have yet to see a type whose C++17 aggregateness led to an actually plausible problem scenario. The real-code examples in the LLVM codebase, as reported on the reflectors, are:

  • A stateless RAII type that only does work in the destructor

  • Two structs that are just a bundle of public data members with a defaulted default constructor and a couple member functions.

  • Two stateless CRTP base classes.

  • Namespaces-masquerading-as-classes, with all static members.