EddieV223
EddieV223

Reputation: 5303

Are there any efficiency issues with brace-init in c++11?

I am considering refactoring a medium sized code base into always using brace-initialization. Are there any efficiency issues I should be aware of?

A few examples could be POD types and built in types, and what about large classes with lots construction parameters?

Upvotes: 4

Views: 220

Answers (1)

Jeffrey Yasskin
Jeffrey Yasskin

Reputation: 5692

This depends on what you mean by "always using brace-initialization". If you convert a constructor like

X x(a, b, c);

into

X x{a, b, c};

(and the behavior doesn't change due to a different constructor getting picked) then the generated code shouldn't get any more or less efficient. On the other hand:

std::vector<std::string> v{
    "long character string a",
    "long character string b",
    "long character string c"};

may well be less efficient than

std::vector<std::string> v;
v.push_back("long character string a");
v.push_back("long character string b");
v.push_back("long character string c");

because of the problem @dyp mentioned, that the vector can't move out of the initializer_list.

Upvotes: 1

Related Questions