mardi 21 mai 2019

Deleting move constructor and constructing object from rvalue

I'm trying to understand Item 17 from "Effective Modern C++" about special member function generation so I was trying some examples and am trying to reason about some behavior. In the book it says:

..that when I refer to a move operation move-constructing or move-assigning a data member or base class, there is no guarantee that a move will actually take place. “Memberwise moves” are, in reality, more like memberwise move requests, because types that aren’t move-enabled (i.e., that offer no special support for move operations, e.g., most C++98 legacy classes) will be “moved” via their copy operations. ... Furthermore, move operations won’t be generated for any class that explicitly declares a copy operation.

The code below errors out if I explicitly delete the move constructor but if I don't the object "s1" gets copy constructed without any errors. Here's a wandbox link to the same code: wandbox link. I guess I'm not understanding the difference between deleting the move constructor and not defining one.

#include <iostream>

struct S
{
    S() = default;
    S(const S&) {
        std::cout << "Copying" << std::endl;
    }
   // S(S&&) = delete;
};

S return_lvalue() {
    S ret{};
    return ret;
}

int main() {
    std::cout << "Hello world" << std::endl;
    // Error here if I delete move constructor
    S s1 = return_lvalue();
}

Aucun commentaire:

Enregistrer un commentaire