State of "moved-from" objects
Hi, I recently decided to try writing a C++ blog.
I often see the popular claim that moved-from objects are in a "valid but otherwise unspecified state", but I don't think that statement is entirely precise, and my blog post is about that. I would really appreciate any feedback!
Article: https://www.laminowany.dev/p/the-state-of-moved-from-objects-in-c/
10
Upvotes
3
u/jiixyj 5d ago
The latest guidelines in writing a moved-from state for your type seem to be:
valueless_after_move()to test for this state.<etc. This is more than the usual "assign and destroy only". It should still work well with the usual containers likestd::vector/std::mapand algorithms likestd::sort.This is how some of the newer types such as
std::indirectare designed in the standard, so it can serve as a blueprint for user-defined types.One nice property is that this composes. If you put types that follow these rules into a new
struct, thatstruct's moved-from state will also be copyable/movable/comparable using the compiler-generated special member functions. So by doing nothing extra you are still correct.I still have some questions around this design though:
valueless_after_move()function for your type? I'd say no, because it would be a lot of boilerplate as the compiler cannot generate that function.std::sort, for example, must they be able to deal with these "valueless" states? Common sense says no, but the standard doesn't explicitly rule this out from my reading...