<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sun, Jan 17, 2016 at 3:50 PM, <Alexander G. Riccio> via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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><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? </span></div></div></div></div></div></div></div></blockquote><div><br></div><div>Some possible reasons:</div><div>(1) push_back often tells readers the type; with emplace* they have to know the type of the lhs to see what's being constructed;</div><div>(2) emplace_back will silently invoke `explicit` constructors, making it possible to accidentally emplace_back a raw pointer into a container of smart pointers, possibly leading to a double-delete when the container takes ownership;</div><div>(3) push_back is more conventional and familiar to more developers.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div dir="ltr"><div><span style="font-size:12.8px">Are there any guidelines as to where push_back should be used instead of emplace_back?</span></div></div></div></div></div></div></div></blockquote><div><br></div><div>If you already have an object of the type you've planning to append, there's no benefit to emplace_back -- it's just an obfuscated way to request moving/copying, which is what push_back does more obviously and safely.</div><div><br></div><div>Personally I reserve emplace_back for times when either it's required by language rules (e.g, a type isn't movable) or it's required for efficiency, and stick to the simpler/safer choice the rest of the time.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div dir="ltr"><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" target="_blank">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" target="_blank">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></div></div></div></div></div></blockquote><div><br></div><div>A nit: push_back moves from temporaries rather than copying, though for some types the two are the same.</div><div><br></div><div>There might be merit in updating for Clang/LLVM code to use emplace_back in some cases, assuming that all of the compilers supported for building are able to handle it. I don't have a strong opinion, though I do caution against overuse of emplace* functions.</div><div><br></div><div>-- James</div><div><br></div></div></div></div>