将 std 绑定传递到函数映射的问题
Problems to pass std bind to map of functions
我使用 std::function
为函数创建了一个映射,当我将普通函数传递给它时它可以工作,但是当我尝试传递std::bind
的结果时我遇到了问题,我会这样做,遵循我的代码:
#include <iostream>
#include <functional>
#include <memory>
#include <map>
#include <tr1/functional>
class A {
int x;
public:
A(int v):x(v){}
int getX() const {return x;}
};
class B {
int x;
public:
B(int v):x(v){}
int getX() const {return x;}
};
class C {
public:
C(){}
std::shared_ptr<A> calc(std::shared_ptr<B> b) {
std::shared_ptr<A> pa (new A(b->getX()));
std::cout << "Class C calc: " << pa->getX() << 'n';
return pa;
}
};
std::shared_ptr<A> calc_out(std::shared_ptr<B> b) {
std::shared_ptr<A> pa (new A(b->getX()*2));
std::cout << "calc_out: " << pa->getX() << 'n';
return pa;
}
int main(int argc, char **argv) {
using namespace std::tr1::placeholders;
C *c = new C();
std::map<int, std::function<std::shared_ptr<A>(std::shared_ptr<B>)>> tmap;
auto icc_fn = std::mem_fn(&C::calc);
auto b_fn = std::bind(icc_fn, c, _1);
tmap[1] = calc_out;
//FIXME: THE PROBLEM IS HERE
tmap[2] = b_fn;
std::shared_ptr<B> pb (new B(10));
std::shared_ptr<A> a1(b_fn(pb));
std::cout << "A1: " << a1->getX() << 'n';
std::shared_ptr<A> a2(tmap[1](pb));
std::cout << "A2: " << a2->getX() << 'n';
return 0;
}
以及您如何看待我以相同的方式b_fn(pb)
和tmap[1](pb)
使用这些函数
当我tmap[1] = b_fn;
注释此行时,程序工作正常,但是当它有此行时,我收到错误:
/home/alex/Tests/cppfunc/main.cpp:48:11: error: no match for ‘operator=’ (operand types are ‘std::map<int, std::function<std::shared_ptr<A>(std::shared_ptr<B>)> >::mapped_type {aka std::function<std::shared_ptr<A>(std::shared_ptr<B>)>}’ and ‘std::_Bind<std::_Mem_fn<std::shared_ptr<A> (C::*)(std::shared_ptr<B>)>(C*, std::tr1::_Placeholder<1>)>’)
tmap[1] = b_fn;
^
/home/alex/Tests/cppfunc/main.cpp:48:11: note: candidates are:
In file included from /home/alex/Tests/cppfunc/main.cpp:2:0:
/usr/include/c++/4.9/functional:2241:7: note: std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(const std::function<_Res(_ArgTypes ...)>&) [with _Res = std::shared_ptr<A>; _ArgTypes = {std::shared_ptr<B>}]
operator=(const function& __x)
^
/usr/include/c++/4.9/functional:2241:7: note: no known conversion for argument 1 from ‘std::_Bind<std::_Mem_fn<std::shared_ptr<A> (C::*)(std::shared_ptr<B>)>(C*, std::tr1::_Placeholder<1>)>’ to ‘const std::function<std::shared_ptr<A>(std::shared_ptr<B>)>&’
/usr/include/c++/4.9/functional:2259:7: note: std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(std::function<_Res(_ArgTypes ...)>&&) [with _Res = std::shared_ptr<A>; _ArgTypes = {std::shared_ptr<B>}]
operator=(function&& __x)
^
/usr/include/c++/4.9/functional:2259:7: note: no known conversion for argument 1 from ‘std::_Bind<std::_Mem_fn<std::shared_ptr<A> (C::*)(std::shared_ptr<B>)>(C*, std::tr1::_Placeholder<1>)>’ to ‘std::function<std::shared_ptr<A>(std::shared_ptr<B>)>&&’
/usr/include/c++/4.9/functional:2273:7: note: std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(std::nullptr_t) [with _Res = std::shared_ptr<A>; _ArgTypes = {std::shared_ptr<B>}; std::nullptr_t = std::nullptr_t]
operator=(nullptr_t)
^
/usr/include/c++/4.9/functional:2273:7: note: no known conversion for argument 1 from ‘std::_Bind<std::_Mem_fn<std::shared_ptr<A> (C::*)(std::shared_ptr<B>)>(C*, std::tr1::_Placeholder<1>)>’ to ‘std::nullptr_t’
/usr/include/c++/4.9/functional:2302:2: note: template<class _Functor> std::function<_Res(_ArgTypes ...)>::_Requires<std::__and_<std::__not_<std::is_same<typename std::decay<_Tp>::type, std::function<_Res(_ArgTypes ...)> > >, std::__or_<std::is_void<_Tp>, std::is_convertible<std::function<_Res(_ArgTypes ...)>::_Invoke<typename std::decay<_Tp>::type>, _Res> > >, std::function<_Res(_ArgTypes ...)>&> std::function<_Res(_ArgTypes ...)>::operator=(_Functor&&) [with _Functor = _Functor; _Res = std::shared_ptr<A>; _ArgTypes = {std::shared_ptr<B>}]
operator=(_Functor&& __f)
^
/usr/include/c++/4.9/functional:2302:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:2311:2: note: template<class _Functor> std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(std::reference_wrapper<_Tp>) [with _Functor = _Functor; _Res = std::shared_ptr<A>; _ArgTypes = {std::shared_ptr<B>}]
operator=(reference_wrapper<_Functor> __f) noexcept
^
/usr/include/c++/4.9/functional:2311:2: note: template argument deduction/substitution failed:
/home/alex/Tests/cppfunc/main.cpp:48:11: note: ‘std::_Bind<std::_Mem_fn<std::shared_ptr<A> (C::*)(std::shared_ptr<B>)>(C*, std::tr1::_Placeholder<1>)>’ is not derived from ‘std::reference_wrapper<_Tp>’
tmap[1] = b_fn;
^
CMakeFiles/cppfunc.dir/build.make:54: recipe for target 'CMakeFiles/cppfunc.dir/main.cpp.o' failed
CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/cppfunc.dir/all' failed
Makefile:117: recipe for target 'all' failed
make[2]: *** [CMakeFiles/cppfunc.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/cppfunc.dir/all] Error 2
make: *** [all] Error 2
您的问题是将<functional>
与<tr1/functional>
混合在一起。如果删除#include <tr1/functional>
并将using namespace std::tr1::placeholders;
更改为using namespace std::placeholders;
代码可以正确编译。(示例。
不幸的是,TR1 标头中的占位符对象与主 <functional>
标头中的std::bind
不兼容;它们将与 std::tr1::bind
一起使用,如果您不能依赖std::bind
在您的实现中正常工作,这是您的替代方案。
相关文章:
- 使用std::函数映射对象方法
- 删除 std::函数映射
- 具有单个参数的布尔函数映射
- 如何使用成员函数映射参数包?(非恢复性,折叠前表达式)
- 如何在类中存储指向字符串到成员函数映射的智能指针,以便它不会因类型不完整而失败?
- 将函数映射提升为字符串
- 将C++成员函数映射到C回调
- 如何使用JNA将返回字符串数组(constchar**)的函数映射到Java
- 如何将可变数量的不同类型的参数传递给 C++11 函数映射中的函数
- 将 std 绑定传递到函数映射的问题
- 异构类型的成员函数映射
- c++的cmath函数映射
- 函数映射容器中C++函数返回类型重载:自动推导失败,并出现编译器错误
- c++构造函数映射对象(已编辑)
- QtConcurrency::与成员函数映射
- c++函数映射实现
- 使用boost的字符串和函数映射会导致编译错误
- 访问与其他函数映射的文件
- 将C++重载函数映射到异构元组上
- 用于成员函数映射的c++find()