1

I want to use a boost::function and pass it to a function to function as a callback. I seem to be having some trouble assigning the member function to it.

The function that I want to pass it to is a static function (as it is invoked on another thread).

boost::function<std::string (ResolverReply& reply)> call_back = std::bind1st(std::mem_fun(&ResolverCommunicator::reply_call_back), *this);

This is inside the ResolverCommunicator class, but my compiler complains about:

_Right: reference to reference is illegal

c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\functional(278): error C2535: 'std::binder1st<_Fn2>::result_type std::binder1st<_Fn2>::operator ()(std::binder1st<_Fn2>::argument_type & ) const' : member function already defined or declared
        with
        [
            _Fn2=std::mem_fun1_t<std::string,ResolverCommunicator,ResolverReply &>
        ]
        c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\functional(272) : see declaration of 'std::binder1st<_Fn2>::operator`()''
        with
        [
            _Fn2=std::mem_fun1_t<std::string,ResolverCommunicator,ResolverReply &>
        ]

I'm then just passing call_back to my static function that is invoked on another thread.

Does anybody know what is wrong?

EDIT:

I have done as the answer says, however now I get this error:

 error C2665: 'boost::bind' : none of the 3 overloads can convert parameter 2 from type 'ResolverCommunicator'
        c:\Program Files\boost\boost_1_44\boost\bind\bind.hpp(1480): could be 'boost::_bi::bind_t<R,F,L> boost::bind<std::string(__thiscall ResolverCommunicator::* )(ResolverReply &),ResolverCommunicator,boost::arg<I>>(F,A1,A2)'
        with
        [
            R=boost::_bi::unspecified,
            F=std::string (__thiscall ResolverCommunicator::* )(ResolverReply &),
            L=boost::_bi::list2<boost::_bi::list_av_2<ResolverCommunicator,boost::arg<1>>::B1,boost::_bi::list_av_2<ResolverCommunicator,boost::arg<1>>::B2>,
            I=1,
            A1=ResolverCommunicator,
            A2=boost::arg<1>
        ]
        c:\Program Files\boost\boost_1_44\boost\bind\bind_mf_cc.hpp(43): or       'boost::_bi::bind_t<R,F,L> boost::bind<std::string,ResolverCommunicator,ResolverReply&,ResolverCommunicator,boost::arg<I>>(R (__thiscall ResolverCommunicator::* )(B1),A1,A2)'
        with
        [
            R=std::string,
            F=boost::_mfi::mf1<std::string,ResolverCommunicator,ResolverReply &>,
            L=boost::_bi::list2<boost::_bi::list_av_2<ResolverCommunicator,boost::arg<1>>::B1,boost::_bi::list_av_2<ResolverCommunicator,boost::arg<1>>::B2>,
            I=1,
            B1=ResolverReply &,
            A1=ResolverCommunicator,
            A2=boost::arg<1>
        ]
        c:\Program Files\boost\boost_1_44\boost\bind\bind_mf_cc.hpp(54): or       'boost::_bi::bind_t<R,F,L> boost::bind<std::string,ResolverCommunicator,ResolverReply&,ResolverCommunicator,boost::arg<I>>(R (__thiscall ResolverCommunicator::* )(B1) const,A1,A2)'
        with
        [
            R=std::string,
            F=boost::_mfi::cmf1<std::string,ResolverCommunicator,ResolverReply &>,
            L=boost::_bi::list2<boost::_bi::list_av_2<ResolverCommunicator,boost::arg<1>>::B1,boost::_bi::list_av_2<ResolverCommunicator,boost::arg<1>>::B2>,
            I=1,
            B1=ResolverReply &,
            A1=ResolverCommunicator,
            A2=boost::arg<1>
        ]
        while trying to match the argument list '(std::string (__thiscall
 ResolverCommunicator::* )(ResolverReply &), ResolverCommunicator, boost::arg<I>)'
        with
        [
            I=1
        ]
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415

1 Answers1

5

It is a known limitation of the standard binders that they do not handle functions which take their parameter by reference. You should consider using boost::bind :

boost::function<std::string (ResolverReply& reply)> call_back =
    boost::bind(&ResolverCommunicator::reply_call_back, this, _1);
icecrime
  • 74,451
  • 13
  • 99
  • 111
  • You can put in this or *this, and boost::bind is clever enough to resolve either. It can even resolve shared_ptr as the 2nd parameter. For the OP, the _1 is a placeholder for the parameter to the function (the ResolverReply&) – CashCow Jan 06 '11 at 10:31
  • @CashCow You're right, and using `*this` is even a bad idea if not wrapped in a `boost::ref()` : answer edited – icecrime Jan 06 '11 at 15:10