Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 绑定一个绑定函数_C++_Function_C++11_Bind - Fatal编程技术网

C++ 绑定一个绑定函数

C++ 绑定一个绑定函数,c++,function,c++11,bind,C++,Function,C++11,Bind,我很难发现为什么这不是编译。我有一个lambda函数,它根据一些参数返回一个std::function 我已经将我的问题缩小到这个片段(它不使用lambdas,但完美地再现了我的错误): 这里,f应该是一些可调用的对象,它将int作为参数,并使用它调用x.bar(int)。另一方面,w只是一个调用some\u-fun(f)的可调用对象,它是f上述可调用对象,具有some\u-fun参数所期望的签名 我错过什么了吗?我可能不知道如何实际混合使用std::bind和std::function som

我很难发现为什么这不是编译。我有一个lambda函数,它根据一些参数返回一个
std::function

我已经将我的问题缩小到这个片段(它不使用lambdas,但完美地再现了我的错误):

这里,
f
应该是一些可调用的对象,它将int作为参数,并使用它调用
x.bar(int)
。另一方面,
w
只是一个调用
some\u-fun(f)
的可调用对象,它是
f
上述可调用对象,具有
some\u-fun
参数所期望的签名


我错过什么了吗?我可能不知道如何实际混合使用
std::bind
std::function

some\u fun
希望参数类型为
const std::function&

bind返回“一个未指定类型T的函数对象”(查看提供的链接,“返回值”部分),您试图将其作为一个有趣的参数传递

这似乎会导致问题,因为不应使用此参数类型

请看:

表达式,就像它们的
boost::bind
前辈一样,支持一种组合操作。您的
w
表达式大致相当于

auto w=std::bind(some_fun,  std::bind(&foo::bar<int>, x, std::placeholders::_1) );

由于
f
实际上没有bind表达式的类型,
std::is_bind_expression::value
将在
f
的类型上为false,因此第二行中的
std::bind
表达式将逐字传递值,而不是尝试应用函数组合规则。

当您用f的
std::function
替换auto时,它似乎起作用。这可能不是您问题的答案。。但是,您是否考虑过使用C++11附带的本机lambda函数(这使得std::bind变得不必要)?在Boost中,我们为此类情况提供了
protect
,但它似乎没有达到标准。
f
不是
std::function
,而是某种无法命名的绑定表达式类型。您必须显式地将其转换为
std::function
。这是您不能使用
auto
的情况之一。请特别参阅关于防止急切求值的部分(您不能不更改类型,因为
std::is_bind_expression
)。这根本不是根本问题--
std::is_bind_expression
,其行为是。
g++ -std=c++0x    test.cpp   -o test
test.cpp: In function ‘int main()’:
test.cpp:20:7: error: no match for call to ‘(std::_Bind<void (*(std::_Bind<std::_Mem_fn<void (foo::*)(int)>(foo, std::_Placeholder<1>)>))(const std::function<void(int)>&)>) ()’
/usr/include/c++/4.6/functional:1130:11: note: candidates are:
/usr/include/c++/4.6/functional:1201:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) [with _Args = {_Args ...}, _Result = _Result, _Functor = void (*)(const std::function<void(int)>&), _Bound_args = {std::_Bind<std::_Mem_fn<void (foo::*)(int)>(foo, std::_Placeholder<1>)>}]
/usr/include/c++/4.6/functional:1215:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const [with _Args = {_Args ...}, _Result = _Result, _Functor = void (*)(const std::function<void(int)>&), _Bound_args = {std::_Bind<std::_Mem_fn<void (foo::*)(int)>(foo, std::_Placeholder<1>)>}]
/usr/include/c++/4.6/functional:1229:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) volatile [with _Args = {_Args ...}, _Result = _Result, _Functor = void (*)(const std::function<void(int)>&), _Bound_args = {std::_Bind<std::_Mem_fn<void (foo::*)(int)>(foo, std::_Placeholder<1>)>}]
/usr/include/c++/4.6/functional:1243:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const volatile [with _Args = {_Args ...}, _Result = _Result, _Functor = void (*)(const std::function<void(int)>&), _Bound_args = {std::_Bind<std::_Mem_fn<void (foo::*)(int)>(foo, std::_Placeholder<1>)>}]
auto w=std::bind(some_fun,  std::bind(&foo::bar<int>, x, std::placeholders::_1) );
std::function<void(int)> f = std::bind(&foo::bar<int>, x, std::placeholders::_1);
auto w = std::bind(some_fun, f);