C++11 在GCC4.7中使用std::bind编译错误

C++11 在GCC4.7中使用std::bind编译错误,c++11,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(f

我在代码的各个地方使用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的类型是绑定了1个int参数的foo的std::function类型,那么为什么bar_fun无法编译呢?我把bar_fun_Levalue包括在内,因为一些谷歌搜索向我展示了这一点。但这并没有解决任何问题

它很像,但太老了,我不认为它有什么关系

gcc的输出并不特别有启发性:

在bindnew.cpp中包含的文件中:1:0: /usr/include/c++/4.7/functional:In“static”Res的实例化 std::_函数_处理程序::_M_invokeconst std::_任何_数据&,_参数类型。。。[带_Res=int;_函子= std::_Bindint,int>;_ArgTypes= {int}]':/usr/include/c++/4.7/functional:2298:6:从 'std::function::function\u Functor,类型名称 std::enable_if::type[带_函子= 标准::_Bindint,int>;_Res=int; _ArgTypes={int};typename std::enable_if::type=std::function::_无用]' bindnew.cpp:15:52:此处为必填项 /usr/include/c++/4.7/functional:1912:40:错误:调用不匹配 'std::_Bindint,int>int' /usr/include/c++/4.7/functional:1140:11:注:候选项包括: /usr/include/c++/4.7/functional:1211:2:note:template\u Result std::\u Bind::operator\u Args&&。。。[带{u Args={u Args…};\u结果= _结果;_Functor=int*int,int;_-Bound_-args={int,std::u-Placeholder}]/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:注意:模板\u结果std::\u绑定::运算符\u参数&&。。。常量[与{u Args={u Args…}; _结果=Result;Functor=int*int,int;Bound_args={int,std::Placeholder}]/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:注意:模板\u结果std::\u绑定::运算符\u参数&&。。。volatile[与{u Args={{u Args…}; _Result=\u Result;\u Functor=int*int,int;\u Bound\u args={int,std::\u Placeholder}]/usr/include/c++/4.7/functional:1239:2:注意: 模板参数扣除/替换失败: /usr/include/c++/4.7/functional:1233:35:错误:无法转换 'std::_No_tuple_element'到参数传递中的'int' /usr/include/c++/4.7/functional:1253:2:note:template\u Result std::\u Bind::operator\u Args&&。。。常量volatile[带_Args={u Args …};_Result=_Result;_Functor=int*int,int;_-Bound_参数= {int,std::u Placeholder}]/usr/include/c++/4.7/functional:1253:2: 注意:模板参数扣除/替换失败: /usr/include/c++/4.7/functional:1247:35:错误:无法转换 'std::_No_tuple_element'到参数传递中的'int'

_2占位符意味着使用返回函子的第二个参数。因此

std::bind(foo,1,_2)
不是std::function,而是

_2占位符意味着使用返回函子的第二个参数。因此

std::bind(foo,1,_2)
不是std::function,而是


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

因此:

等等

这意味着您可以在std::bind创建的对象中切换参数,只需执行以下操作

auto bar_auto=std::bind(foo,_2,_1);

调用bar_auto对象时,第一个参数将是foo的第二个参数,调用中的第二个参数将是foo的第一个参数。

占位符位置对象,例如,当您使用_2时,不是调用函数中参数的位置,而是创建的可调用对象中参数的占位符。相反,总是从_1开始,然后增加

因此:

等等

这意味着您可以在std::bind创建的对象中切换参数,只需执行以下操作

auto bar_auto=std::bind(foo,_2,_1);

调用bar_auto对象时,第一个参数将是foo的第二个参数,调用中的第二个参数将是foo的第一个参数。

谢谢,这就把事情弄清楚了。获取bar_auto的编译器警告可能会有所帮助。“我犯了这样一个愚蠢的错误,没有理由不引起警告吗?”@Marclaesen,是这样的,有点像,因为我犯了一个又长又难读的错误不过,那个垃圾堆不是关于bar_auto的。我想没有警告,因为std::bind的结果函数可能有任意数量的参数,所以在我的例子中,无法评估给定的占位符是否不合适2。谢谢
s、 这就把事情弄清楚了。获取bar_auto的编译器警告可能会有所帮助。“我犯了这样一个愚蠢的错误,没有理由不引起警告吗?”@Marclaesen,是这样的,有点像,因为我犯了一个又长又难读的错误不过,那个垃圾堆不是关于bar_auto的。我想没有警告,因为std::bind的结果函数可能有任意数量的参数,所以在我的例子中,无法评估给定的占位符是否不合适。
auto bar_auto=std::bind(foo,_2,_1);