If I remove the copy constructor, can't I get the implicit move constructor?

The following command will not compile (tested by both clang and gcc)

#include <vector>

struct Foo
{
    Foo(int a=0) : m_a(a) {}
    Foo(const Foo& f) = delete;
    // Foo(Foo&& f) = default;
private:
    int m_a;
};

int main()
{
    std::vector<Foo> foovec;
    foovec.emplace_back(44); // might resize, so might move
}

      

But if I don't remove the copy constructor, or if the move constructor is used by default,
this works. Thus, deleting the copy constructor disables the move constructor,
and why is this rational?

+3


source to share


1 answer


You should see a table about the special members of the class. When you set the copy constructor to be remote, the move constructor is not automatically generated.

More details in the table:



enter image description here

Source (slides) .

+6


source







All Articles