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

6

u/EdwinYZW Dec 19 '23

I would do: const auto tax = 0.09 * price;

-16

u/snowflake_pl Dec 19 '23

Why not static constexpr? 🙂

15

u/yu_yaki Dec 19 '23

because price might not be constexpr? Why static though?

-9

u/snowflake_pl Dec 19 '23

Maybe static doesn't make a difference for a small variable but was it large, giving it static storage ensures compile time initialization and prevents unnecessarily copying the data to stack

4

u/yu_yaki Dec 19 '23

It's still irrelevant to the specific case that you commented under. I was wondering if you had some more to say, but oh well. I do get your point though.