samedi 3 novembre 2018

Why is this deleted move constructor accepted by the compiler?

In the following code example, I have no particular reason to delete the move constructor, but I don't understand why it compiles (x is an object that prints whether it is moved or copied).

class foo {
    X x;

public:
    foo()=default;
    foo(const foo &)=default;
    foo(foo &&)=delete;
};

int main()
{
    vector<foo> v;
    foo a;

    v.push_back(a);
    v.push_back(a);
}

The second time push_back is called on the std::vector, it relocates the already existing object, usually with a move operation. With the foo move constructor declared 'default', I can see this happen.

However, when the move constructor is explicitly deleted, I'd expect compilation to fail since it is still available for overload, but deleted.
It does compile and the vector reallocation uses the copy constructor.

What is happening here ?

Aucun commentaire:

Enregistrer un commentaire