在gcc 4.7中使用std :: bind编译错误

Mar*_*sen 1 c++11

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);

}
Run Code Online (Sandbox Code Playgroud)

毫无疑问的类型bar_autostd::function<int(int)>(类型foo1个int参数绑定),所以为什么bar_fun编译失败?我包括bar_fun_lvalue因为一些谷歌搜索向我显示rvalues曾经是有问题的.但这并没有解决任何问题.

它类似于这个bug,但是它太旧了我不认为它是相关的.

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 = {int}]':/ 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 = {int}; 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 = {_ 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:错误:无法将参数传递中的'std :: _ No_tuple_element'转换为'int'/usr/include/c++/4.7/functional:1225:2:注意:模板_Result std :: _ Bind <_Functor(_Bound_args ... )> :: operator()(_ Args && ...)const [with _Args = {_ 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:错误:无法在传递的参数中将'std :: _ No_tuple_element'转换为'int'/usr/include/c++/4.7/functional:1239:2:注意:模板_Result std :: _ Bind <_Functor(_Bound_args ... )> :: operator()(_ Args && ...)volatile [with _Args = {_ 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 = {_ 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'

Som*_*ude 6

占位符位置对象(例如,当您使用时_2)不是您调用的函数中参数的位置,而是创建的可调用对象中参数的占位符.相反,始终开始_1并增加.

所以:

auto bar_auto=std::bind(foo,1,_1);
Run Code Online (Sandbox Code Playgroud)

等等


这意味着您std::bind只需执行类似操作即可切换创建的对象中的参数

auto bar_auto=std::bind(foo,_2,_1);
Run Code Online (Sandbox Code Playgroud)

当你"调用" bar_auto对象时,第一个参数将是第二个参数foo,而调用中的第二个参数将是第一个参数foo.