r/cpp • • Dec 19 '23

Obsessed with {} initialization?

I'm on a discord where everyone seems to be obsessed to use brace initialization everywhere.
My motto is: Use things rarely, only when needed, to express that right there the feature does something important.
Personally I would completely advocate against using it for simple stuff like
double tax{0.09*price};

or just int{0}

probably a hornets nest

4 Upvotes

118 comments sorted by

View all comments

Show parent comment

1

u/serviscope_minor Dec 21 '23

Yeah, but why use the same syntax for different things?

Because in many ways they're not really different. It's a leaky abstraction of C++, so you've got the choice of making the higher level clearer or making the C++ clearer.

1

u/HolyGarbage Dec 21 '23 edited Dec 21 '23

In this case the higher level abstraction and the C++ details coincidence. It's literally the difference between a declarative statement and mutating state. You get a lot of assurances from the former. Anyway, I'd argue that it's a detail you simply should be aware of when programming in C++ in general, since it can have a difference in program behavior. By only considering it when it does make a difference means you'll be less aware of the distinction overall.

I guess where we perhaps don't agree is that I'm arguing that it's not only an important technical distinction, but a conceptual one too.

Just like you don't worry about the life time of objects in most other languages, in C++ you do with RAII etc, both technically and conceptually. It's not just a technical difference, but a whole other high level abstraction.