std :: bind到std :: function?

时间:2012-07-07 06:34:27

标签: c++ function c++11 bind std

我使用这个得到编译错误:

std::vector<std::function<int(int)>> functions;

std::function<int(int, int)> foo = [](int a, int b){ return a + b; };
std::function<int(int)> bar = std::bind(foo, 2);

functions.push_back(bar);

错误是:

  

/ usr / include / c ++ / 4.6 / functional:1764:40:错误:无法调用'(std :: _ Bind(int)&gt;)(int)'

有人可以告诉我如何将std::bind转换为std::function吗?

1 个答案:

答案 0 :(得分:20)

std::function<int(int)> bar = std::bind(foo, 2, std::placeholders::_1);