<div dir="ltr">There are many places in clang where I see code like this:<div><pre style="font-family:Consolas;color:gainsboro;background:rgb(30,30,30)"><span style="color:rgb(218,218,218)">Checkers</span><span style="color:rgb(180,180,180)">.</span><span style="color:rgb(200,200,200)">push_back</span><span style="color:rgb(180,180,180)">(</span><span style="color:rgb(78,201,176)">CheckerInfo</span><span style="color:rgb(180,180,180)">(</span><span style="color:rgb(127,127,127)">fn</span><span style="color:rgb(180,180,180)">,</span> <span style="color:rgb(127,127,127)">name</span><span style="color:rgb(180,180,180)">,</span> <span style="color:rgb(127,127,127)">desc</span><span style="color:rgb(180,180,180)">));</span>
</pre></div><div>The equivalent use of emplace_back would be:</div><div><div><pre style="font-family:Consolas;color:gainsboro;background:rgb(30,30,30)"><span style="color:rgb(218,218,218)">Checkers</span><span style="color:rgb(180,180,180)">.</span><span style="color:rgb(200,200,200)">emplace_back</span><span style="color:rgb(180,180,180)">(</span><span style="color:rgb(127,127,127)">fn</span><span style="color:rgb(180,180,180)">,</span> <span style="color:rgb(127,127,127)">name</span><span style="color:rgb(180,180,180)">,</span> <span style="color:rgb(127,127,127)">desc</span><span style="color:rgb(180,180,180)">);</span></pre></div></div><div><div><div><div class="gmail_signature"><div dir="ltr"><div><span style="font-size:12.8px">....which is (a) more concise, and (b) more efficient. Is there any reason why clang uses push_back instead of emplace_back? Are there any guidelines as to where push_back should be used instead of emplace_back?</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">If not, is there any reason to keep using push_back? Why not just replace all uses of push_back with emplace_back?</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">In llvm/tools/clang/lib alone, I count (via grep -r -n -I push_back lib|wc -l) 5,421 uses of push_back. </span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Not all of those are for STL containers (not many of the llvm containers have emplace_back), but surely this is some low-hanging (performance) fruit? If not in release builds (where push_back's copy constructor call is likely elided), then in debug builds?</span><br></div><div dir="ltr"><span style="font-size:12.8px"><br></span></div><div>Background info on <a href="http://en.cppreference.com/w/cpp/container/vector/emplace_back">emplace_back</a> for those who are unfamiliar:</div><div>emplace_back <a href="http://stackoverflow.com/questions/4303513/push-back-vs-emplace-back/4306581#4306581">in-place-constructs (placement new) an element instead of creating a temporary</a> and then copy constructing (push_back does this).</div><div dir="ltr"><span style="font-size:12.8px"><br></span></div><div dir="ltr"><span style="font-size:12.8px">Sincerely,</span><br style="font-size:12.8px"><span style="font-size:12.8px">Alexander Riccio</span><br style="font-size:12.8px"><span style="font-size:12.8px">--</span><br style="font-size:12.8px"><span style="font-size:12.8px">"Change the world or go home."</span><div style="font-size:12.8px"><a href="http://about.me/ariccio" target="_blank">about.me/ariccio</a></div><div style="font-size:12.8px"><a href="http://about.me/ariccio" target="_blank"><br></a></div><div style="font-size:12.8px">If left to my own devices, I will build more.</div><div style="font-size:12.8px">⁂</div></div></div></div></div>
</div></div></div>