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

deW1
  • 5,562
  • 10
  • 38
  • 54
  • Look up C++11 uniform initialization. [Mildly related question](http://stackoverflow.com/questions/7612075/how-to-use-c11-uniform-initialization-syntax) – Borgleader May 07 '15 at 19:49

1 Answers1

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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720