4

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?

sp2danny
  • 7,488
  • 3
  • 31
  • 53
  • http://en.cppreference.com/w/cpp/language/move_constructor and http://stackoverflow.com/questions/8283589/are-move-constructors-produced-automatically – Karoly Horvath Nov 07 '14 at 14:16
  • @KarolyHorvath Why does it need the copy or move-constructor when it is constructing a `Foo` in-place? – David G Nov 07 '14 at 14:26
  • @0x499602D2: it's needed for vector resizing, there might be not enough space when you `emplace_back` – Karoly Horvath Nov 07 '14 at 15:41

1 Answers1

12

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

JFMR
  • 23,265
  • 4
  • 52
  • 76
Dakorn
  • 883
  • 6
  • 11