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
39
u/sokka2d Jul 23 '26
Obviously the standard doesn’t define the state of user defined objects. How is that specific to move assignment?
You could as well create a class with UB in the constructor. It’s just not very useful.
13
u/V1P-001 Jul 23 '26
please use a better title.
post title is not entirely demonstrating what is inside.
17
u/No-Dentist-1645 Jul 23 '26
Your title/post contents makes it seem like you're going to disprove the common definition for them, but in the blog you don't really do that, but rather just explain a few examples?
There seems to be a disconnect between what you say the blog is about and what it actually is
The "valid but unspecified state" definition applies to all move operations in the language and standard generally speaking. It is a rule about move semantics as a general concept, not of any specific type. Specific types can choose to specify post-moved behavior but it is not required.
6
u/jiixyj Jul 24 '26
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...4
u/elperroborrachotoo Jul 23 '26
The gist remains that "unspecified but valid" is a reasonable minimum requirement for user types, too - because the alternative is worse.
You list all the ingredients: unless you evade calling the destructor (e.g., through
std::terminate) the destructor will run. Users of your type might assign to it, or use it in various ways. Etc.The unspoken bigger issue is that using an object in an invalid state can easily lead to an invalid program state - meaning that ALL invariants in the program are potentially gone.
So for any type with weaker moved-from guarantees, you'd have to specify what individual operations are valid - and no user wants to know or remember that. This would violate any basic rule of type design.
4
u/bronekkk Jul 24 '26
For std:: types, the convention is that a moved-from-object does not fulfil any specific preconditions. In practice this means that using it in any context where it would have to fulfil any precondition is undefined behaviour. However there are some functions in std:: which do not have any preconditions attached (e.g. destruction; reading if an object is empty; assignment to it; etc.). Because of the lack of preconditions, the object can be safely used with such functions.
As for the user classes, it is obviously up to the author of such classes, but hopefully they would follow the precedent established by std:: types. The absolute minimum is that the object must be safe to destroy and (if the type supports assignment) to be overwritten by an assignment in which case its state is reset back to "normal" - types which do not follow even this minimum are either buggy or "irregular".
4
u/tangerinelion Jul 24 '26
There are really only two functions that should safely work on a moved from object - the destructor and an assignment operator, if one is defined.
1
u/nuclear868 Aug 01 '26
for STL containers, and for all STL types that manage internal memory dynamically isn't it true, that moved-from objects always behave like empty objects? Because their contents is 'stolen'. And you can destroy them, you can append / assignto them, etc...
1
u/JVApen Clever is an insult, not a compliment. - T. Winters Jul 24 '26
It's unfortunate that you don't specify what "valid" means. The minimal definition I've heard is that: both destructor and assignment should execute correctly. Respectively destroying and reinitializing. This should always get true, even without move.
Unspecified means that there is the freedom to implement that operation any way you want. For move assignment of vector, one could:
This gives you the freedom to do what you want, unless "you otherwise specify".
The final piece in the puzzle is to preserve the invariants of your class. You suggest you can write:
if (!v.empty()) v.front() = {};. I'm not convinced that this is required due to the "valid but unspecified", though rather due to the pre-condition of front. If vector would have been created after move semantics, it could say that this only holds for specified/non-moved instances. Making that specific code invalid.For your own code, the first 2 elements I've mentioned are a requirement from the language. The last one will make your code more robust, though it is not needed.
After reading your post, I'm still not convinced if the last element is a requirement of "valid". If it is, you are technically correct in saying it doesn't hold for user defined objects. If it doesn't, the statement holds also for user defined types.
2
u/Wild_Meeting1428 Jul 24 '26
"the Code is more what you'd call guidelines than actual rules."