I have a couple of questions about move assignment operator and possibly thrown exceptions:
- Provided that
is_nothrow_move_assignable<T>::valueis true for the typeT, is it enough to conclude that move assignment operator for that type will never throw an exception? In other words, is it ok to have thisnoexceptspecification:
struct MyClass
{
MyClass& operator = (MyClass&& other) noexcept(std::is_nothrow_move_assignable<OtherClass>::value)
{
this->data = std::move(other.data);
return *this;
}
OtherClass data;
};
(Let's assume that we cannot rely on defaulted move-assignment operator)
- Talking specifically about
std::vector, in C++11 its move-assignment operator may throw implementation-defined exceptions (according to the docs). And even since C++17 itsnoexceptspecification still depends on the allocator traits. But this is a general case. Let's say we have two instrances ofstd::vector<double>with default allocator. Is it still possible that move assignment throws an exception in this case?
std::vector<double> v1{ 1,2,3 }, v2{4,5};
v1 = std::move(v2); // can it throw?