Alex
Alex

Reputation: 3381

Problems to pass std bind to map of functions

I created a map for functions using std::function, it works when I pass a normal function to it, but when I try to pass a result of std::bind I have problems, I would to do that, follows my code:

#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;
}

And how you can see I use the functions b_fn(pb) and tmap[1](pb) the same way

When I comment this line tmap[1] = b_fn; the program works ok, but when it has this line I get the error:

/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

Upvotes: 1

Views: 969

Answers (1)

ecatmur
ecatmur

Reputation: 157344

Your problem is mixing <functional> with <tr1/functional>. If you remove #include <tr1/functional> and change using namespace std::tr1::placeholders; to using namespace std::placeholders; your code compiles correctly. (Example.)

Unfortunately, the placeholder objects in the TR1 headers are not compatible with the std::bind in the main <functional> header; they would work with std::tr1::bind, which is your alternative if you can't rely on std::bind working correctly in your implementation.

Upvotes: 2

Related Questions