1

While writing this answer I realized that it's a bit unclear to me how is a move-assignment from a moved-from value to a moved-from value specified in C++ standard library.

As mentioned in that answer, for any MoveAssignable type its fine to perform such a move.

Is it fine to perform such a move for any standard library type with a move assignment operator? What about move-construction from a moved-from object? Is it always valid in the standard library?

I guess it's easier to see that it's valid for types that specify their moved-from state, like e.g. unique_ptr, but what about those that don't specify it?

In other words, I guess the question would be, is every stdlib type that has a move-ctor MoveContructible, and is every stdlib type that has a move-assign MoveAssignable?

deshalder
  • 507
  • 2
  • 13
  • _"...Unless otherwise specified, all standard library objects that have been moved from are placed in a "valid but unspecified state", meaning the object's class invariants hold (so __functions without preconditions__, such as the assignment operator, can be safely used on the object after it was moved from):..."_ https://en.cppreference.com/w/cpp/utility/move (I don't have a list of the exceptions). – Richard Critten Jun 18 '22 at 13:01
  • @RichardCritten doesn't this talk about the object itself, not about the assignment argument? Like I can assign to `x` after moving from `x`, but can I assign `x` to `y` after moving from `x`? – deshalder Jun 18 '22 at 13:05
  • @deshalder I don't think that you can assign `x` to `y` after moving from `x` in the context you're talking. – Jason Jun 18 '22 at 13:06
  • 1
    @HolyBlackCat `std::swap(x, x)` typically does a self-move-assignment from a moved-from value to a moved-from value (`lhs = std::move(rhs);`) (which is fine as Howard Hinnant says for any MoveAssignable type) – deshalder Jun 18 '22 at 13:12
  • This comes close _"...However, self-move-assignment of standard library types is guaranteed to place the object in a valid (but usually unspecified) state:..."_ `v = std::move(v); // the value of v is unspecified` also from https://en.cppreference.com/w/cpp/utility/move – Richard Critten Jun 18 '22 at 13:15

0 Answers0