In the following code:
struct X
{
X() = default;
X(const X&) { printf("copy construct\n"); }
X& operator=(const X&) { printf("copy assign\n"); return *this; }
X(X&&) { printf("move construct\n"); }
X& operator=(X&&) { printf("move assign\n"); return *this; }
// Replacing the above two lines with these lines below causes a compile error.
// X(X&&) = delete;
// X& operator=(X&&) = delete;
};
void f(X x) {}
int main()
{
X x;
std::function<void (X)> fx(f);
f(x);
return 0;
}
if I define struct X to have copy and move operations, then a std::function with signature void (X) is able to bind to it. But if I delete the move operations, the code doesn't compile any more, with this error:
prog.cc:26:29: error: no matching constructor for initialization of 'std::function<void (X)>'
std::function<void (X)> fx(f);
candidate template ignored: requirement '__callable<void (*&)(X), false>::value' was not satisfied [with _Fp = void (*)(X)]
function(_Fp);
I'm just trying to understand why are the move operations required if the signature describes a function where X is passed by value?