dimanche 25 septembre 2016

about the move-assignment operator in 《C++ primer》

A move-assignment operator examples:

StrVec & StrVec :: operator = (StrVec && rhs) noexcept
{
    if (this! = & rhs) {
        free (); // release the old space
        elements = rhs.elements; // takeover resources
        first_free = rhs.first_free;
        cap = rhs.cap;
        rhs.elements = rhs.first_free = rhs.cap = nullptr; // set destructible
    }
    return * this;
}

However, in another Message class also has a move-assignment operator:

Message & Message :: operator = (Message && rhs)
{
    if (this! = & rhs) {// check self-assignment
        remove_from_Folders ();
        contents = std :: move (rhs.contents); // move-assignment operator
        move_Folders (& rhs); // Reset Floders point to this Message
    }
    return * this;
}

One thing I do not understand is: In StrVec

 elements = rhs.elements; 

you can "steal" resources。So rhs.element is an rvalue, then why in the Message

contents = std :: move (rhs.contents);

why add std::move? Just do not rhs.contents ? ? Or std::move is displayed in order to show more obvious the right value? ? ? ?

Aucun commentaire:

Enregistrer un commentaire