std::bind转为std::function?

28

我使用这段代码时出现了编译错误:

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: error: no match for call to '(std::_Bind(int)>) (int)'

有谁能告诉我如何将std::bind转换为std::function

1个回答

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

std::bind(foo, 2, std::placeholders::_1) 等同于 std::bind(foo, 2,) 吗? - BlackMamba
@BlackMamba 我觉得你对哪个函数有两个参数感到困惑了。除了 _1 之外的占位符意味着 绑定结果 有多个参数。 - Caleth
这是我本周遇到的第二个问题/答案,其中提供的答案使用了 std::placeholders::...。在这一周之前,我并不熟悉这个东西。学习新知识总是好的。 - Francis Cugler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接