Why move constructor for std::vector
with custom allocator does not deduce a noexcept()
from allocator's behaviours?
This leads to the class that encapsulates such vector cannot form the (other) vector that can be normally moved in some <algorithm>
s. Even if the underlying type meets the nessesary requirements (MoveInsertable and DefaultInsertable).
noexcept is nice for two reasons: The compiler can optimize a little better because it doesn't need to emit any code for unwinding a call stack in case of an exception, and. It leads to incredible performance differences at runtime for std::vector (and other containers, too)
The copy constructor for an object thrown as an exception must be declared noexcept , including any implicitly-defined copy constructors.
I assume that by "move constructor for std::vector
with custom allocator" you mean the allocator-extended move constructor i.e. this constructor:
vector(vector&& v, const allocator_type& a);
The main reason is that if v.get_allocator() != a
then the constructor must allocate more memory, which could throw bad_alloc
. There is no way to know at compile-time if two allocators of a given type will always compare equal or not (I have reported this as a defect, see LWG 2108).
N.B. the standard does not require this constructor or the vector(vector&&)
move constructor to be noexcept
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With