<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jan 17, 2015 at 6:15 AM, Jean-Daniel Dupas <span dir="ltr"><<a href="mailto:dev@xenonium.com" target="_blank">dev@xenonium.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div id=":ilj" class="a3s" style="overflow:hidden">> Le 16 janv. 2015 à <span class="aBn" tabindex="0"><span class="aQJ">18:48</span></span>, David Blaikie <<a href="mailto:dblaikie@gmail.com">dblaikie@gmail.com</a>> a écrit :<br>
<span class="">><br>
><br>
><br>
> On Fri, Jan 16, 2015 at 3:02 AM, Jean-Daniel Dupas <<a href="mailto:dev@xenonium.com">dev@xenonium.com</a>> wrote:<br>
> Just a question not directly related to that change, but similar. Is there something that prevent the use of vector.emplace_back() in the lld code base (compiler or supported c++ library issue) ?<br>
><br>
> There is some places where we just create a new object and push it back in a vector and could simplify them using emplace_back() instead.<br>
><br>
> Personally I wouldn't use "emplace_back(new T())" on a container of unique_ptr<T> because of exception safety, etc (though we don't use exceptions, it's habits that make me more comfortable reading code, etc). The risk is that emplace_back could fail (when trying to allocate more space, for example) and then leak the T* before it ever created a unique_ptr<T> around it.<br>
><br>
> So I prefer "push_back(make_unique<T>())" (& in general I prefer make_unique wherever possible, it means I don't have to think hard about whether the 'new' is used safely, etc)<br>
><br>
<br>
</span>Of course. I was thinking about simplifying construct like:<br>
<br>
  Section temp;<br>
  file.sections.push_back(std::move(temp));<br>
<br>
or<br>
<br>
  pages.push_back(UnwindInfoPage());</div></blockquote></div><br>Personally I find emplace_back significantly harder to read for little benefit. It hides the constructor call behind a fairly magical forwarding function. I would generally rather see a constructor call explicitly. The only time this bothers me is when it isn't really a constructor at all but just an aggregate thing. There, I would rather use list initialization, but we can't because of MSVC 2012.</div><div class="gmail_extra"><br></div><div class="gmail_extra">The times when emplace_back seems useful is when there is a specific reason that you need to construct things in place. But that comes up quite rarely in my experience.</div></div>