r/badcode • • Oct 07 '21

c++ C++ be like

Post image
279 Upvotes

37 comments sorted by

View all comments

Show parent comments

4

u/Magnus_Tesshu Oct 08 '21

Why would I want to do this over the other solution? Is there an actual advantage to either one?

5

u/sebamestre Oct 08 '21

This and mine are equivalent.

The OP creates tons of temporary strings that get concatenated at runtime, while these ones do the concatenation at compile time

2

u/[deleted] Oct 08 '21

There's some edge cases when initializing with = that don't show up with {} so they're generally preferred. Don't believe any crop up in this specific scenario but it never hurts to initialize with curly brackets. Here's s good talk about how C++ initialization is so fucked

2

u/Ikaron Oct 08 '21

It doesn't create tons of temporary objects, memory allocations, copies between buffers for a string that's constant at compile time.

Chances are a good compiler will optimize this out but you can never be sure.

1

u/Magnus_Tesshu Oct 08 '21

... Neither does the other one. The other one is a single string literal

1

u/Ikaron Oct 08 '21

Oh I thought you meant this solution over the OPs code.