<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">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="gmail-t-lc" style="color:rgb(0,0,0);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">std::vector::resize</a></span><span style="color:rgb(0,0,0);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="color:rgb(0,0,0);font-family:DejaVuSans,"DejaVu Sans",arial,sans-serif;font-size:12.8px">If an exception occurs during this operation, </span><span class="gmail-t-lc" style="color:rgb(0,0,0);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">std::vector::resize</a></span><span style="color:rgb(0,0,0);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="color:rgb(0,0,0);font-size:12.8px;font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace">std::move_if_noexcept</code><span style="color:rgb(0,0,0);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="color:rgb(0,0,0);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">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">libcxx-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/libcxx-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/libcxx-dev</a><br>
</blockquote></div></div>