在gcc 4.7中使用std :: bind编译错误 [英] Compile errors using std::bind in gcc 4.7
问题描述
在代码的不同位置使用 std :: bind
时遇到很多麻烦。有时它会起作用,有时却不会,所以我认为我做的事情根本上是错误的。
I am having a lot of trouble using std::bind
in various places of my code. Sometimes it works, sometimes it doesn't, so I assume I am doing something fundamentally wrong.
据我了解,以下 std :: bind
的基本用法应该可以正常工作:
As far as I understand, the following basic use of std::bind
should work fine:
#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
,因为一些谷歌搜索显示我右值曾经是有问题的。但这并不能解决任何问题。
Surely the type of bar_auto
is std::function<int(int)>
(type of foo
with 1 int
argument bound), so why does bar_fun
fail to compile? I included bar_fun_lvalue
because some googling showed me that rvalues used to be problematic. but this didn't fix anything.
它类似于此错误,但是它太旧了,我不希望它具有相关性。
It resembles this bug, but that's so old I don't expect it to be relevant.
gcc的输出不是特别有启发性:
gcc's output is not particularly enlightening:
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,类型名
std :: enable_if<(!std :: is_integral< _Functor> :: value),
std :: function< _Res(_ArgTypes ...)> :: __ Useless> ::类型)[with _Functor =
std :: _ Bind))(int,int)>; _Res = int;
_ArgTypes = {int};类型名称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:错误:无法在传递
的参数中将
'std :: _ No_tuple_element'转换为'int'/usr/include/c++/4.7/functional: 1253:2:注意:模板_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'
In file included from bindnew.cpp:1:0: /usr/include/c++/4.7/functional: In instantiation of ‘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: required from ‘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: required from here /usr/include/c++/4.7/functional:1912:40: error: no match for call to ‘(std::_Bind))(int, int)>) (int)’ /usr/include/c++/4.7/functional:1140:11: note: candidates are: /usr/include/c++/4.7/functional:1211:2: note: template _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: note:
template argument deduction/substitution failed: /usr/include/c++/4.7/functional:1206:35: error: cannot convert ‘std::_No_tuple_element’ to ‘int’ in argument passing /usr/include/c++/4.7/functional:1225:2: note: template _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: note:
template argument deduction/substitution failed: /usr/include/c++/4.7/functional:1219:35: error: cannot convert ‘std::_No_tuple_element’ to ‘int’ in argument passing /usr/include/c++/4.7/functional:1239:2: note: template _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: note:
template argument deduction/substitution failed: /usr/include/c++/4.7/functional:1233:35: error: cannot convert ‘std::_No_tuple_element’ to ‘int’ in argument passing /usr/include/c++/4.7/functional:1253:2: note: template _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: note: template argument deduction/substitution failed: /usr/include/c++/4.7/functional:1247:35: error: cannot convert ‘std::_No_tuple_element’ to ‘int’ in argument passing
推荐答案
占位符位置对象(例如,当您使用 _2
时)不是参数的位置在您调用的函数中,但在创建的可调用对象中为参数的占位符。相反,总是从 _1
开始并增加。
The placeholder position objects (e.g. when you use _2
) is not the position of the argument in the function you call, but a placeholder for the argument in the created callable object. Instead always start with _1
and increase.
所以:
auto bar_auto=std::bind(foo,1,_1);
等。
这意味着您可以通过简单地操作
This means you can switch the arguments in the object created by std::bind
by simply doing like
auto bar_auto=std::bind(foo,_2,_1);
调用 bar_auto
对象,第一个参数将是 foo
的第二个参数,调用中的第二个参数将是 foo $ c $的第一个参数c>。
When you "call" the bar_auto
object, the first argument will be the second argument to foo
, and the second argument in the call will be the first argument to foo
.
这篇关于在gcc 4.7中使用std :: bind编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!