1

Lately I have been seeing quite a few people who initialize their std::strings like this:

std::string a{ "test" }; // Yes they do it with a single value

Now I wouldn't use this unless it's an array or to pass an initialization list.
But it got me curious, is there any point of doing this preferably over:

std::string a = "test";
std::string a ( "test" );

All three work without a doubt and I get the difference between the latter two.

Does it give some kind of performance improvement?

Code

1

1 Answer 1

2

Does it give some kind of performance improvement?

No.

In the end, one std::string constructor gets called. The only difference is how the compiler goes about choosing the constructor.

Developers will probably choose a style according to usage with classes where it DOES matter, perhaps because there are initialization-list constructors, or because of narrowing conversions or explicit constructors. And then use the same style for strings just for the sake of consistency.

For std::string all three paths through the compiler end up choosing the same constructor.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.