Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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?

like image 757
sp2danny Avatar asked Dec 14 '22 18:12

sp2danny


1 Answers

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).

like image 135
Dakorn Avatar answered Jan 25 '23 23:01

Dakorn