sp2danny
sp2danny

Reputation: 7687

If I delete the copy constructor, do I get no implicit move constructor?

The following wont compile (tried both clang & 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 delete the copy constructor, or if I default the move constructor,
it will work. So, does deleting copy constructor suppress move constructor,
and what is the rational behind that?

Upvotes: 4

Views: 1450

Answers (1)

Dakorn
Dakorn

Reputation: 903

You should see table about special class members. When you set copy constructor as a deleted one move constructor won't be generated automatically.

See more in table:

enter image description here

Source (slides).

Upvotes: 12

Related Questions