<div dir="auto">The only reason we call the copy constructor is to provide the strong exception safety guarantee (which arguably may have been a mistake). Without exceptions the guarantee is satisfied trivially. <div dir="auto"><br></div><div dir="auto">I think we should move the objects. Doing the more efficient thing provides value to the user. <div dir="auto"><br></div><div dir="auto">Google compiles a ton of code without exceptions. I will experiment with making `move_if_noexcept` always move and report back.</div><div dir="auto"><br></div><div dir="auto">/Eric</div><div dir="auto"><br></div><div dir="auto"><br></div><br><br><div class="gmail_quote" dir="auto"><div dir="ltr" class="gmail_attr">On Wed., Apr. 17, 2019, 1:12 p.m. Louis Dionne, <<a href="mailto:ldionne@apple.com">ldionne@apple.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><br><div><br><blockquote type="cite"><div>On Apr 17, 2019, at 12:39, Duncan P. N. Exon Smith <<a href="mailto:dexonsmith@apple.com" target="_blank" rel="noreferrer">dexonsmith@apple.com</a>> wrote:</div><br class="m_-4687556771311550736Apple-interchange-newline"><div><div style="word-wrap:break-word;line-break:after-white-space">I'm curious about how people feel this should/could behave with -fno-exceptions.  IIRC, Libc++ calls the copy constructor (following the standard), but -fno-exceptions isn't really standard, and from a user perspective that doesn't make sense.  `noexcept` is essentially meaningless with -fno-exceptions.<br></div></div></blockquote><div><br></div><div>I personally feel like it would be surprising if -fno-exceptions caused the move constructor to be called instead of the copy constructor. My mental model is that -fno-exceptions only disables exceptions as a language feature, but doesn't really instruct libraries to provide a different API (even though the difference in this case is very small). Instead, I feel like it would make more sense if "everything" was implicitly marked noexcept when -fno-exceptions is used (because that's really the case). The library would behave optimally with -fno-exceptions without having to do anything special.</div><div><br></div><div>Louis</div><br><blockquote type="cite"><div><div style="word-wrap:break-word;line-break:after-white-space"><div><br></div><div>Thoughts?</div><div><br><blockquote type="cite"><div>On Apr 16, 2019, at 23:19, Eric Fiselier via libcxx-dev <<a href="mailto:libcxx-dev@lists.llvm.org" target="_blank" rel="noreferrer">libcxx-dev@lists.llvm.org</a>> wrote:</div><br class="m_-4687556771311550736Apple-interchange-newline"><div><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Apr 16, 2019 at 2:25 PM Michael Narigon via libcxx-dev <<a href="mailto:libcxx-dev@lists.llvm.org" target="_blank" rel="noreferrer">libcxx-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">std:vector is using the element copy constructor on existing elements during its resizing operation in reserve(). I would expect it to use the move constructor for efficiency. Is this intended behavior?<br></blockquote><div><br></div><div>Yes, this behavior is mandated by the standard. The copy constructor is used when the move constructor is not noexcept. From cppreference's `std::move_if_noexcept` documentation:<br><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="m_-4687556771311550736gmail-t-lc" style="font-size:12.8px;font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace"><a href="https://en.cppreference.com/w/cpp/container/vector/resize" title="cpp/container/vector/resize" style="text-decoration-line:none;color:rgb(11,0,128);background:none" target="_blank" rel="noreferrer">std::vector::resize</a></span><span style="font-family:DejaVuSans,"DejaVu Sans",arial,sans-serif;font-size:12.8px">, which may have to allocate new storage and then move or copy elements from old storage to new storage.<br></span><span style="font-family:DejaVuSans,"DejaVu Sans",arial,sans-serif;font-size:12.8px">If an exception occurs during this operation, </span><span class="m_-4687556771311550736gmail-t-lc" style="font-size:12.8px;font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace"><a href="https://en.cppreference.com/w/cpp/container/vector/resize" title="cpp/container/vector/resize" style="text-decoration-line:none;color:rgb(11,0,128);background:none" target="_blank" rel="noreferrer">std::vector::resize</a></span><span style="font-family:DejaVuSans,"DejaVu Sans",arial,sans-serif;font-size:12.8px"> undoes everything it did to this point, which is only possible if<br></span><code style="font-size:12.8px;font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace">std::move_if_noexcept</code><span style="font-family:DejaVuSans,"DejaVu Sans",arial,sans-serif;font-size:12.8px"> was used to decide whether to use move construction or copy construction. (unless copy constructor is</span> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-family:DejaVuSans,"DejaVu Sans",arial,sans-serif;font-size:12.8px">not available, in which case move constructor is used either way and the strong exception guarantee may be waived)</span>  </blockquote><div> <a href="https://en.cppreference.com/w/cpp/utility/move_if_noexcept" target="_blank" rel="noreferrer">https://en.cppreference.com/w/cpp/utility/move_if_noexcept</a></div><div><br></div><div>If you add `noexcept` to your move constructor, vector should call the move constructor.</div><div><br></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
This is with the libc++ delivered with Xcode 10.1.<br>
<br>
Attached is a sample program displaying the behavior.<br>
_______________________________________________<br>
libcxx-dev mailing list<br>
<a href="mailto:libcxx-dev@lists.llvm.org" target="_blank" rel="noreferrer">libcxx-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/libcxx-dev" rel="noreferrer noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/libcxx-dev</a><br>
</blockquote></div></div>
_______________________________________________<br>libcxx-dev mailing list<br><a href="mailto:libcxx-dev@lists.llvm.org" target="_blank" rel="noreferrer">libcxx-dev@lists.llvm.org</a><br><a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/libcxx-dev" target="_blank" rel="noreferrer">https://lists.llvm.org/cgi-bin/mailman/listinfo/libcxx-dev</a><br></div></blockquote></div><br></div></div></blockquote></div><br></div></blockquote></div></div></div>