<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">2016-12-30 11:34 GMT+01:00 Chandler Carruth <span dir="ltr"><<a href="mailto:chandlerc@gmail.com" target="_blank">chandlerc@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><span class="gmail-"><div dir="ltr">On Fri, Dec 30, 2016 at 2:08 AM Piotr Padlewski via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr" class="gmail-m_-7306051220478311565gmail_msg">Thanks for very accurate responses.<div class="gmail-m_-7306051220478311565gmail_msg">- I totally agree what Dave and Chandler said about explicit and implicit operations, this is what I meant my first email.</div><div class="gmail-m_-7306051220478311565gmail_msg"> I believe there are places like </div><div class="gmail-m_-7306051220478311565gmail_msg"> v.emplace_back(A, B);</div><div class="gmail-m_-7306051220478311565gmail_msg"> istead of</div><div class="gmail-m_-7306051220478311565gmail_msg"> v.push_back(make_pair(A, B));b</div><div class="gmail-m_-7306051220478311565gmail_msg"> That can make code simpler.</div></div></blockquote><div><br></div></span><div>Do you have examples? The only ones i can come up with are the ones where the push_back variant literally can't compile because the type isn't movable.</div><div><br></div><div>Perhaps it would be useful to break down categories of can happen here...</div><div><br></div><div>Case 1: there is one object already -- this is a *conversion* of a type.</div><div>- If the author of the conversion made it *implicit*, then 'v.push_back(x)' just works.</div><div>- If the author of the conversion made it *explicit* I would like to see the name of the type explicitly: 'v.push_back(T(x))'.</div><div><br></div></div></div></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div></div><div>Case 2a: There is a collection of objects that are being composed into an aggregate. We don't have any interesting logic in the constructor, it takes an initializer list.</div><div>- This should work with 'v.push_back({a, b, c})'</div><div>- If it doesn't today, we can fix the type's constructors so that it does.</div><div>- Using 'emplace_back' doesn't help much -- you still need {}s to form the std::initializer_list in many cases. Pair and tuple are somewhat unusual in not requiring them.</div><div><br></div></div></div></blockquote><div> This sounds extremely reasonable. </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div></div><div>Case 2b: A specific constructor needs to be called with an argument list. These arguments are not merely being aggregated but are inputs to a constructor that contains logic.</div><div>- This is analogous to a case called out w.r.t. '{...}' syntax in the coding standards[1]</div><div>- Similar to that rule, I would like to see a *call to the constructor* rather than hiding it behind 'emplace_back' as this is a function with interesting logic.</div><div>- That means i would write T(a, b, c) anyways, and 'v.push_back(T(a, b, c))' works.</div><div><br></div></div></div></blockquote><div>Calling emplace_back with 0 or multiple arguments is a clear way of saying "this constructor takes multiple arguments".</div><div>We can do it with initializer list with easy way like:</div><div><font face="monospace, monospace">v.emplace_back() == v.push_back({})</font></div><div><font face="monospace, monospace">v.emplace_back(a, b ,c) == v.push_back({a, b, c})</font></div><div><br></div><div>I personally never liked the initializer syntax because of tricky casees like:</div><div><br></div><div><font face="monospace, monospace">vector<string> v{{"abc", "def"}};</font><br></div><div>Which is equivalent of </div><div><font face="monospace, monospace">vector<string> v = {std::string("abc", "def")};</font><br></div><div>That will call std::string ctor with 2 iterators likely crashing, and putting same string might gives us empty string.</div><div><br></div><div>In this case programmer probably meant</div><div><font face="monospace, monospace">std::vector<std:string> v({"abc", "def"});</font><br></div><div>or</div><div><font face="monospace, monospace">std::vector<std::string> v = {"abc", "def"};</font><br></div><div><br></div><div>But this case is not possible to mess up with push_back (in the case of vector<vector<string>> or something). At least I hope it is not.</div><div>So avoiding braces is my personal preference. It is fine for me if we would choose to prefer '<font face="monospace, monospace">v.push_back({a, b, c})</font>' instead of '<font face="monospace, monospace">v.emplace_back(a, b, c)</font>', ofc as long as most of the community would prefer first form to the second :)</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div></div><div>[1]: <a href="http://llvm.org/docs/CodingStandards.html#do-not-use-braced-initializer-lists-to-call-a-constructor" target="_blank">http://llvm.org/docs/<wbr>CodingStandards.html#do-not-<wbr>use-braced-initializer-lists-<wbr>to-call-a-constructor</a></div><div><br></div><div>Case 3: Passing objects of type 'T' through 'push_back' fails to compile because they cannot be copied or moved.</div><div>- You *must* use 'emplace_back' here. No argument (obviously).</div><div><br></div><div>My experience with LLVM code and other codebases is that case 3 should be extremely rare. The intersection of "types that cannot be moved or copied" and "types that you put into containers" is typically small.</div><div><br></div><div><br></div><div>Anyways, I don't disagree with this point with a tiny fix:</div><span class="gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr" class="gmail-m_-7306051220478311565gmail_msg"><div class="gmail-m_-7306051220478311565gmail_msg"> I think in cases like this we can leave it for judgement of contributor.</div></div></blockquote></span><div>*or reviewer*. ;]</div><div><br></div><div>I continue to think exceptions can be made in rare cases when folks have good reasons. But I expect this to be quite rare. =]</div></div></div>
</blockquote></div><br></div><div class="gmail_extra">Piotr</div></div>