1

More particularly:

struct A{
    std::list<int> list;
    std::list<int> foo(){
        return std::move(list);
    }
}

A a;
// insert some elements into a.list
a.foo(); // is this guaranteed to clear a.list?

Is the last line above guaranteed to leave a.list empty?

1
  • You mentioned std::move(). Are you planning to entertain return value or not? Commented Feb 13, 2020 at 4:02

4 Answers 4

4

No. Moving from most standard library classes leaves them in a "valid but unspecified state" [1]. That means you have to explicitly clear a.list in order to ensure that it's empty after the move.

[1] There are exceptions to this rule: most notably, std::unique_ptr is required to be null after a move.

6
  • So std::vector and std::list are not among those exceptions?
    – Museful
    Commented Feb 11, 2020 at 19:11
  • @Museful Right. I don't think any of the standard containers guarantee a particular state after a move, other than std::array since its move constructor is the implicit one.
    – Brian Bi
    Commented Feb 11, 2020 at 19:12
  • @Museful to be sure they are empty just do list = {} after moving it. Commented Feb 11, 2020 at 19:13
  • @GuillaumeRacicot Yes but that requires two additional lines of code if the list is being returned from a function, or is there another way?
    – Museful
    Commented Feb 11, 2020 at 19:15
  • 5
    @Museful return std::exchange(list, {});
    – Brian Bi
    Commented Feb 11, 2020 at 19:18
1

Is the last line above guaranteed to leave a.list empty?

No. The standard says following:

[lib.types.movedfrom]

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.

And this is the definition of valid but unspecified state:

[defns.valid]

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

[ Example: If an object x of type std​::​vector is in a valid but unspecified state, x.empty() can be called unconditionally, and x.front() can be called only if x.empty() returns false. — end example ]

std::list specification adds no further guarantees for the move constructor.

0

No, you must clear it otherwise it is left in an "unspecified state" - as per the documentation on std::list

0

Perhaps it is worth explaining why list might not be empty afterward.

If the move ends up being a copy, for example because of incompatible allocators and the allocator is not moved, then the list may still have the old elements.

Although vector is (inconsistently) required to be cleared in all cases, there is support in the Committee for requiring that the capacity of the assigned-to vector be moved back to the right-side operand when that would be compatible with the allocator situation.

2
  • However, notice that std::list<int> cannot possibly have "incompatible allocators." Even if you changed OP's code to use std::pmr::list<int>, this code would still be fine, because there's no possible way for a move-construction (out of this->list, into the return slot) to involve "incompatible allocators" — there is only one allocator involved. The short answer is "yes, a.list must become empty (because there is physically no way for it to have any elements in it after the move has succeeded)." Commented Feb 13, 2020 at 1:36
  • Regarding "the capacity of the assigned-to vector be moved back to the right-side operand": YES, that is pragmatic and makes total sense, and allows people to finally see the use case of that "valid but unspecified" state moved from objects are to be left in. You can now check .capacity() and have a nice surprise: you have allocated memory ready to be used, and you didn't even need to ask for it!
    – jrsala
    Commented Feb 22, 2020 at 1:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.