If I define a move constructor and don't need a copy constructor, why would I need to define a copy constructor?

36 Views Asked by At

Using Apple clang version 12.0.0 (clang-1200.0.32.29) -std=c++17, the following code does not compile

struct C {
  C()  {}
  C(C&& other) {std::cout << "move\n";}
  //C(const C& other) {std::cout << "copy\n";}
  void operator()(float x) {}
};

int main(){
  std::function<void(float)> f = C();
}

unless I uncomment the copy constructor. Yet when I run the executable, I find out that the copy constructor is never called.

Does the standard require that a copy constructor be defined in this scenario? If so, why? If not, why does the code not compile?

I have resisted adding a copy constructor because, in the code from which this minimal example was derived, the class has a member of type std::unique_ptr.

0

There are 0 best solutions below