dimanche 22 février 2015

What are the rules for noexcept on default defined move constructors?

Especially in connection with std::vector it is important that types are noexcept movable when possible.


So when declaring a move constructor = default like in



struct Object1
{
Object1(Object1 &&other) = default;
};


std::is_nothrow_move_constructible<Object1>::value will be true as every member (0 here) of Object1 is nothrow-move-constructible, which is answered here.


Yet what happens if the move copy constructor is only declared and then later = default defined like in the following code?



struct Object2
{
Object2(Object2 &&other);
};
Object2::Object2(Object2 &&other) = default;


With g++ 4.9.2 std::is_nothrow_move_constructible<Object2>::value is false and I have to mark both the declaration and the definition as noexcept to make it true.


Now what I am interested in is what the actual rules are. Especially since Item 22 in Effective Modern C++ (Scott Meyers) seems to give ill advice by suggesting to implement the pimpl-idiom move constructor like I did with Object2.


Aucun commentaire:

Enregistrer un commentaire