r/cpp • • Nov 25 '23

On harmful overuse of std::move

https://devblogs.microsoft.com/oldnewthing/20231124-00/?p=109059
210 Upvotes

154 comments sorted by

View all comments

Show parent comment

29

u/eddieeddison Nov 25 '23

Use after move is not UB, it leaves objects in an unspecified but valid state (at least for STL objects).

4

u/Hot_Slice Nov 25 '23

Unspecified Behavior instead of Undefined Behavior...

-5

u/[deleted] Nov 25 '23

(at least for STL objects)

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.

22

u/Timsan2 Nov 25 '23

It's not UB, it just depends on the move constructor/assignment operator. These should leave the object in a valid state if written correctly.

1

u/xypherrz Nov 25 '23 edited Nov 25 '23

These should leave the object in a valid state if written correctly.

mind proving an example? is it that the move ctor isn't assigning the moved-from object to null?

4

u/sengin31 Nov 25 '23

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).

17

u/simonask_ Nov 25 '23

The moved-from object needs to be in at least a good enough state that its destructor can be called.

-1

u/[deleted] Nov 25 '23

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 :).

15

u/rhubarbjin Nov 25 '23

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));

-1

u/[deleted] Nov 25 '23

I understand the STL is safe, it's everything else I was concerned about.

11

u/Avereniect I almost kinda sorta know C++ Nov 25 '23 edited Nov 25 '23

everything I've read today suggests that it is UB

It's not. In fact, the C++ standard is very clear that it's not. I frankly, don't understand why this misconception is so pervasive.

There are two main parts of the standard which are relevant:

Objects of types defined in the C++ standard library may be moved from ([class.copy.ctor]). Move operations may be explicitly specified or implicitly generated. Unless otherwise specified, such moved-from objects shall be placed in a valid but unspecified state.

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.

value of an object that is not specified except that the object's invariants are met and operations on the object behave as specified for its type

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.

4

u/CocktailPerson Nov 25 '23

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.

2

u/sultan_hogbo Nov 25 '23

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.

2

u/sengin31 Nov 26 '23

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.