49

When implementing move constructors and move assignment operators, one often writes code like this:

p = other.p;
other.p = 0;

The implicitly defined move operations would be implemented with code like this:

p = std::move(other.p);

Which would be wrong, because moving a pointer variable does not set it to null. Why is that? Are there any cases were we would like the move operations to leave the original pointer variable unchanged?

Note: By "moving", I do not just mean the subexpression std::move(other.p), I mean the whole expression p = std::move(other.p). So, why is there no special language rule that says "If the right hand side of an assignment is a pointer xvalue, it is set to null after the assignment has taken place."?

18
  • 5
    Why should there be? The only thing you're supposed to do with a 'moved' object is ignore it. Having a pointer point to memory not owned by the class shouldn't be a problem if you no longer use the pointer, right?
    – user743382
    Feb 26, 2012 at 11:06
  • 5
    "You don't pay for what you don't use"?
    – Bo Persson
    Feb 26, 2012 at 11:09
  • 9
    @hvd: The destructor surely won't ignore it if it says delete p :) Feb 26, 2012 at 11:10
  • 5
    @FredOverflow And an automatically generated destructor will never say delete p, so what is the point you're trying to make?
    – user743382
    Feb 26, 2012 at 11:29
  • 6
    std::swap on two pointers is an excellent example of where you don't want std::move to auto-NULL pointers. And what is the "null" state for integers? True, an optimizer could solve the std::swap case to be ideal again, but I think such cases show that we better leave it alone. Feb 26, 2012 at 12:18

5 Answers 5

38

Setting a raw pointer to null after moving it implies that the pointer represents ownership. However, lots of pointers are used to represent relationships. Moreover, for a long time it is recommended that ownership relations are represented differently than using a raw pointer. For example, the ownership relation you are referring to is represented by std::unique_ptr<T>. If you want the implicitly generated move operations take care of your ownership all you need to do is to use members which actually represent (and implement) the desired ownership behavior.

Also, the behavior of the generated move operations is consistent with what was done with the copy operations: they also don't make any ownership assumptions and don't do e.g. a deep copy if a pointer is copied. If you want this to happen you also need to create a suitable class encoding the relevant semantics.

2
  • 4
    I just remembered reading a paper that said "moving should never be more expensive than copying" or something like that. Setting the original pointer null would break that rule. Do you know which paper I'm talking about, or did my brain make that up? Feb 26, 2012 at 14:23
  • I'm not completely fine with the first sentence "Setting a raw pointer to null after moving it implies that the pointer represents ownership." Why is that? I disagree. In a modern code base heap ownership is handled by smart pointers. Raw pointers are merely equally to a reference, except that the developer has the option to reference to "nothing". In an old code base you do not know if a pointer is used for owning heap or just referencing something. Thus setting a pointer to null IMHO does not imply owning it.
    – Don Pedro
    Jul 3, 2019 at 14:36
8

Moving renders the moved-from object "invalid". It does not automatically set it to a safe "empty" state. In accordance with C++'s long-standing principle of "don't pay for what you don't use", that's your job if you want it.

4

I think the answer is : implementing such a behavior yourself is pretty much trivial and hence the Standard didn't feel any need to impose any rule on the compiler itself. The C++ language is huge and not everything can be imagined before its use. Take for example, C++'s template. It was not first designed to be used the way it is used today (i.e it's metaprogramming capability). So I think, the Standard just gives the freedom, and didn't make any specific rule for std::move(other.p), following one of it's the design-principle: "You don't pay for what you don't use".

Although, std::unique_ptr is movable, though not copyable. So if you want pointer-semantic which is movable and copyable both, then here is one trivial implementation:

template<typename T>
struct movable_ptr
{
    T *pointer;
    movable_ptr(T *ptr=0) : pointer(ptr) {} 
    movable_ptr<T>& operator=(T *ptr) { pointer = ptr; return *this; }
    movable_ptr(movable_ptr<T> && other) 
    {
        pointer = other.pointer;
        other.pointer = 0;
    }
    movable_ptr<T>& operator=(movable_ptr<T> && other) 
    {
        pointer = other.pointer;
        other.pointer = 0;
        return *this;
    } 
    T* operator->() const { return pointer; }
    T& operator*() const { return *pointer; }

    movable_ptr(movable_ptr<T> const & other) = default;
    movable_ptr<T> & operator=(movable_ptr<T> const & other) = default;
};

Now you can write classes, without writing your own move-semantics:

struct T
{
   movable_ptr<A> aptr;
   movable_ptr<B> bptr;
   //...

   //and now you could simply say
   T(T&&) = default; 
   T& operator=(T&&) = default; 
};

Note that you still have to write copy-semantics and the destructor, as movable_ptr is not smart pointer.

8
  • 3
    @Fred - It would if you used a sufficiently smart pointer instead of storing raw pointers. Could that be the problem?
    – Bo Persson
    Feb 26, 2012 at 11:15
  • @BoPersson: I think that should be an answer? Feb 26, 2012 at 11:17
  • 1
    @BoPersson That and "You don't pay for what you don't use" would make an excellent answer. Feb 26, 2012 at 11:56
  • @FredOverflow: Do implicitly defined move operators call std::move? Feb 26, 2012 at 12:39
  • 1
    Move operations are simply defined to call the move operations of the members, see 12.8 §15. Feb 26, 2012 at 12:57
0

For example, if you have a pointer to a shared object. Remember, that after moving an object must remain in an internally consistent state, so setting a pointer that must not be null to a null value is not correct.

I.e.:

struct foo
{
  bar*  shared_factory;  // This can be the same for several 'foo's
                         // and must never null.
};

Edit

Here is a quote about MoveConstructibe from the standard:

T u = rv;
...
rv’s state is unspecified [ Note:rv must still meet the requirements
of the library component that is using it. The operations listed in
those requirements must work as specified whether rv has been moved
from or not.
8
  • But setting the pointer variable to null would only happen on xvalues. The client would not be able to inspect the null pointer afterwards. Feb 26, 2012 at 11:06
  • What if your destructor, for instance, uses the pointer somehow and will invoke UB if it is null? Also, I can't back up my claim with a quote from the standard, but as far as I remember, after moving object must remain valid (in that you can continue use it normally), not just destructable.
    – user319799
    Feb 26, 2012 at 11:09
  • @FredOverflow: found relevant quote in the standard.
    – user319799
    Feb 26, 2012 at 11:13
  • @doublep: The MoveConstructible requirement is only for the standard containers, nothing else. Also, you define the "internal consisten state" of your objects.
    – Xeo
    Feb 26, 2012 at 11:19
  • @Xeo: Even if so, it would be pretty weird if C++ stdlib contained features (nulling moved-out pointers) that'd go against its own requirements for other parts.
    – user319799
    Feb 26, 2012 at 11:21
0

I think what makes the difference here is a fully blown object on the one hand and a POD on the other hand.

  • For objects either the implementer specifies what move construction and move assignment should do or the compiler generates a default. The default is to call the move constructors/assignment operators of all member.
  • For POD's (and a pointer is a POD) C++ inherits from C and nothing is done with it when not explicitely coded. It's just the same behavior as POD members in a class are treated in a constructor. If you don't explicitely put them in the initializer list, well, then they stay uninitialized and are a source of potential bugs. AFAIK this is even valid for compiler generated constructors! That's why I took on the habit of generally initializing all members to be on the safe side.

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.