4

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?

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
EddieV223
  • 5,085
  • 11
  • 36
  • 38
  • there is a tool called `clang-modernize` that ships with `clang` that pretty much does what you asked, not really a bulletproof solution, but it's probably worth more than just a try . http://clang.llvm.org/extra/clang-modernize.html – user2485710 Mar 16 '14 at 17:06
  • What makes you think it might be less efficient to use one or the other form of initialization? Also, since brace initialization is unambiguous and therefore considered "the best", you can assume that if a compiler implements them differently, care will be taken to make _these_ the most efficient. – Damon Mar 16 '14 at 18:17
  • 3
    I find `int x = 1;` more readable than `int x{1};`. Not everything looks better with braces. – DanielKO Mar 16 '14 at 18:21
  • _Why_ are you doing this? – Lightness Races in Orbit Mar 16 '14 at 18:32
  • "Why" I'm doing this is off topic. I didn't ask if I "should" be doing this. That would be a different question. – EddieV223 Mar 16 '14 at 18:36
  • @DanielKO you make a good point with that. I probably won't change the built in types. – EddieV223 Mar 16 '14 at 19:15
  • 1
    @EddieV223 [Herb Sutter even recommends](http://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style-almost-always-auto/) `auto x = 1;` or `auto x = int{1};`. `std::initializer_list` has possible efficiency issues (since you cannot move *out* of it), see e.g. http://stackoverflow.com/questions/8193102/initializer-list-and-move-semantics but braced-init-lists vs. direct constructor calls? Not that I'm aware of (there are other issues, as those initialization styles are not equivalent). – dyp Mar 16 '14 at 19:59
  • @dyp Oh I still remember the controversy when he said that out loud ("use auto unless you have a good reason not to"), many eyebrows were raised, Scott shook his head and said "noooooo" and rolled his eyes... after that Herb didn't mention it again. – DanielKO Mar 17 '14 at 04:38
  • @DanielKO Was that before or after that advertisement blog post? (And are there any good refutations of it?) – dyp Mar 17 '14 at 13:10
  • @dyp See this [C++ and Beyond 2012 panel](http://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012-Scott-Andrei-and-Herb-Ask-Us-Anything), at around the 25 min mark. – DanielKO Mar 17 '14 at 14:21
  • @DanielKO Hmm The GotW is more recent (2013). Maybe it has been written *because* `auto` has a bad reputation amongst other high-profile programmers? – dyp Mar 17 '14 at 14:58

1 Answers1

1

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.

Jeffrey Yasskin
  • 5,171
  • 2
  • 27
  • 39