[libcxx-dev] std::vector using copy constructor on existing elements during reserve()
Eric Fiselier via libcxx-dev
libcxx-dev at lists.llvm.org
Tue Apr 16 23:19:49 PDT 2019
On Tue, Apr 16, 2019 at 2:25 PM Michael Narigon via libcxx-dev <
libcxx-dev at lists.llvm.org> wrote:
> 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?
Yes, this behavior is mandated by the standard. The copy constructor is
used when the move constructor is not noexcept. From cppreference's
> <https://en.cppreference.com/w/cpp/container/vector/resize>, which may
> have to allocate new storage and then move or copy elements from old
> storage to new storage.
> If an exception occurs during this operation, std::vector::resize
> <https://en.cppreference.com/w/cpp/container/vector/resize> undoes
> everything it did to this point, which is only possible if
> std::move_if_noexcept was used to decide whether to use move construction
> or copy construction. (unless copy constructor is
not available, in which case move constructor is used either way and the
> strong exception guarantee may be waived)
If you add `noexcept` to your move constructor, vector should call the move
> This is with the libc++ delivered with Xcode 10.1.
> Attached is a sample program displaying the behavior.
> libcxx-dev mailing list
> libcxx-dev at lists.llvm.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the libcxx-dev