std::function copying parameters?

Viewed 1069

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?

2 Answers
Related