std::function copying parameters?
问题 My code: #include <iostream> #include <functional> using namespace std; struct A { A() = default; A(const A&) { cout << "copied A" << endl; } }; void foo(A a) {} int main(int argc, const char * argv[]) { std::function<void(A)> f = &foo; A a; f(a); return 0; } I'm seeing "copied A" twice on the console. Why is the object being copied twice, not once? How can I prevent this properly? 回答1: The specialization std::function<R(Args...)> has a call operator with the following declaration: R operator