我在代码的各个地方使用 std::bind
时遇到了很多麻烦 . 有时候它会起作用,有时却不起作用,所以我认为我做的事情根本就是错误的 .
据我了解, std::bind
的以下基本用法应该可以正常工作:
#include <functional>
int foo(int a, int b){ return a+b; }
int main(){
using namespace std::placeholders;
// works
auto bar_auto=std::bind(foo,1,_2);
// compile error
std::function<int(int)> bar_fun=std::bind(foo,1,_2);
int quux=1;
// compile error
std::function<int(int)> bar_fun_lvalue=std::bind(foo,quux,_2);
}
当然 bar_auto
的类型是 std::function<int(int)>
( foo
的类型与1 int
参数绑定),为什么 bar_fun
无法编译?我包括 bar_fun_lvalue
因为一些谷歌搜索向我展示了rvalues used to be problematic . 但这并没有解决任何问题 .
它类似于this bug,但's so old I don' t期望它是相关的 .
gcc的输出不是特别有启发性:
在bindnew.cpp中包含的文件中:1:0:/ usr / include / c /4.7/functional:在'static _Res std :: _ Function_handler <_Res(_ArgTypes ...),Functor> :: _ M_invoke(const)的实例化中std :: _ Any_data&, ArgTypes ...)[with _Res = int; _Functor = std :: _ Bind))(int,int)>; _ArgTypes =]':/ usr / include / c /4.7/functional:2298:6:从'std :: function <_Res(_ArgTypes ...)> :: function(_Functor,typename std :: enable_if)中需要<(!std :: is_integral <_Functor> :: value),std :: function <_Res(_ArgTypes ...)> :: _ Useless> :: type)[with _Functor = std :: _ Bind))(int,int )>; _Res = int; _ArgTypes = ; typename std :: enable_if <(!std :: is_integral <_Functor> :: value),std :: function <_Res(_ArgTypes ...)> :: _ Useless> :: type = std :: function :: _ Useless]' bindnew.cpp:15:52:从这里需要/ usr / include / c /4.7/functional:1912:40:错误:无法匹配调用'(std :: _ Bind))(int,int)>)(int )'/ usr / include / c /4.7/functional:1140:11:注意:候选人是:/ usr / include / c /4.7/functional:1211:2:注意:模板_Result std :: _ Bind <_Functor(Bound_args . ..)> :: operator()( Args && ...)[with _Args = ; _Result = _Result; _Functor = int(*)(int,int); _Bound_args = {int,std :: _ Placeholder <2>}] / usr / include / c /4.7/functional:1211:2:注意:模板参数扣除/替换失败:/ usr / include / c /4.7/functional:1206 :35:错误:无法在传递/ usr / include / c /4.7/functional:1225:2的参数中将'std :: _ No_tuple_element'转换为'int':注意:模板_Result std :: _ Bind <_Functor(Bound_args ... )> :: operator()( Args && ...)const [with _Args = ; _Result = _Result; _Functor = int(*)(int,int); _Bound_args = {int,std :: _ Placeholder <2>}] / usr / include / c /4.7/functional:1225:2:注意:模板参数扣除/替换失败:/ usr / include / c /4.7/functional:1219 :35:错误:无法在传递/ usr / include / c /4.7/functional:1239:2的参数中将'std :: _ No_tuple_element'转换为'int':注意:模板_Result std :: _ Bind <_Functor(Bound_args ... )> :: operator()( Args && ...)volatile [with _Args = ; _Result = _Result; _Functor = int(*)(int,int); _Bound_args = {int,std :: _ Placeholder <2>}] / usr / include / c /4.7/functional:1239:2:注意:模板参数扣除/替换失败:/ usr / include / c /4.7/functional:1233 :35:错误:无法在传递/ usr / include / c /4.7/functional:1253:2的参数中将'std :: _ No_tuple_element'转换为'int':注意:模板_Result std :: _ Bind <_Functor(Bound_args ... )> :: operator()( Args && ...)const volatile [with _Args = ; _Result = _Result; _Functor = int(*)(int,int); _Bound_args = {int,std :: _ Placeholder <2>}] / usr / include / c /4.7/functional:1253:2:注意:模板参数扣除/替换失败:/ usr / include / c /4.7/functional:1247 :35:错误:在参数传递中无法将'std :: _ No_tuple_element'转换为'int'
2 回答
占位符位置对象(例如,当您使用
_2
时)不是您调用的函数中参数的位置,而是创建的可调用对象中参数的占位符 . 相反,始终以_1
开头并增加 .所以:
等等
这意味着您只需执行类似操作即可切换
std::bind
创建的对象中的参数当
bar_auto
对象时,第一个参数将是foo
的第二个参数,而调用中的第二个参数将是foo
的第一个参数 ._2
占位符表示使用返回的仿函数的第二个参数 . 因此的类型不是
std::function<int(int)>
但是要获得
std::function<int(int)>
,请使用