将std绑定传递给函数映射的问题

时间:2014-12-02 14:56:23

标签: c++ c++11 stl functional-programming bind

我使用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

1 个答案:

答案 0 :(得分:2)

您的问题是<functional><tr1/functional>混合。如果您删除#include <tr1/functional>并将using namespace std::tr1::placeholders;更改为using namespace std::placeholders;,则代码会正确编译。 (Example。)

不幸的是,TR1标头中的占位符对象与主std::bind标头中的<functional>不兼容;他们会与std::tr1::bind合作,如果您不能依赖std::bind在您的实施中正常工作,那么这是您的替代方案。