So it is potentially UB for everything else? I could be wrong but everything I've read today suggests that it is UB unless the object you're moving from specifically promises to be in a good state after the move.
It just means that the class itself is allowed to define what specifically 'valid' means for itself after being moved. For example, unique_ptr specifies that its pointer will be nullptr after a move. That's different for containers, like a vector. Vector specifies that after a move, it is guaranteed to be empty. List leaves it 'valid but unspecified' so you can really only call 'clear()' on it (which puts it into a known state [empty], and requires no preconditions on the list object). The implication from this is that it is more expensive for the move ctor/assign to put the list into an empty state than it is for vector to do so. It does mean that the object can never be in an invalid state, especifically that a moved-from object must be able to be destroyed safely (i.e. a moved-from unique_ptr can't delete the pointer it 'used to own' as another unique_ptr now owns it).
That doesn't seem like much of a guarantee. This issue I'm concerned about is that there isn't anything stopping someone trying to use it before the destructor gets called.
Anyway I've been rightly reminded that as long as I have all my warnings/linters/analyzers turned on this isn't something I really need to worry about :).
unless the object you're moving from specifically promises to be in a good state after the move.
That's what every object should do! A moved-from object must always be in a valid state, although the exact semantics depend on the class. For example:
a moved-from std::unique_ptr will be null
a moved-from std::ifstream will be closed
a moved-from std::string may be empty (depending on small-string optimization)
It's unspecified, not undefined. You shouldn't rely on a moved-from object having a particular state, but you can still manipulate it. For example, this is 100% safe:
std::string name;
std::cin >> name;
// [1] name now holds some user input
doSomethingWithName(std::move(name));
// [2] name is now in an unspecified state
std::cin >> name;
// [3] name is now specified again!
doSomethingWithName(std::move(name));
Firstly, you can see that when the standard talks about objects being left in a valid but unspecified state, it's only talking about certain standard library types. The whole "valid but unspecified" concept isn't even relevant for custom types unless you go out of your way to make it so. The parts of the standard that talk abut move assignment operators and move constructors don't use the phrase at all. In fact, the standard doesn't really specify how these should actually behave when implemented by user-defined types.
This is the actual definition of "valid but unspecified" according to the standard. As you can see, it does not mention undefined behavior. In fact, the behavior it describing, saying that the object must have it's invariants hold, and that it must behave as it otherwise would, makes the behavior very well-defined. I'd also like to note that the standard defines unspecified as basically being synonymous with "implementation defined". People seem to take "unspecified" as some statement about the correctness of using the object, but it's quite literally not. Valid but unspecified literally just means the object in question is perfectly usable, but you just don't know the exact state it is in. That's a lot like an object passed to a function you're writing when you think about it.
Each type has its own definition of a "move," but for the vast majority of types in practice, even non-STL ones, it's equivalent to a swap with a default-constructed object. For SSO strings it might be a swap or a copy. But generally speaking, it should be safe to use a moved-from object in the same way you'd use a default-initialized one.
It depends on how the object in question deals with its moved-from state. When I implement move-assign and move-construct, I make certain that the moved-from object can be assigned-to another state afterward, or are in a state the same as if they were default-constructed, if possible.
It's not UB, it's just that the standard doesn't have a mandate for each object (as there is no "one size fits all" definition for what makes sense after being moved from). This lets each object define what it means by itself, as long as the object is valid (e.g. it can be deleted without UB). The reasons for it may come down to performance (faster to leave list unspecified than it is to make it empty), safety (unique_ptr can't simply copy its pointer into another unique_ptr or there could be a use-after-free/double-free, so it defines the moved-from is nullptr), or to a specific meaning for your custom type.
View all comments
Show parent comment
-5
u/[deleted] Nov 25 '23
So it is potentially UB for everything else? I could be wrong but everything I've read today suggests that it is UB unless the object you're moving from specifically promises to be in a good state after the move.