相关疑难解决方法(0)

std :: function复制参数?

我的代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

我在控制台上看到"复制A"两次.为什么对象被复制两次而不是一次?我怎样才能正确预防?

c++ c++11 std-function

9
推荐指数
1
解决办法
782
查看次数

标签 统计

c++ ×1

c++11 ×1

std-function ×1