Search code examples
c++functionboostboost-bind

Pass a std::function as template parameter to another function does not compile


I develop in C+03 using Boost. I would like to bind a function class member. Then pass this to a generic wrapper as template parameter and inside the wrapper to call the real function . But I cannot make this to compile.

Below is a demo code

#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <functional>
#include <iostream>

class Request;
class Request2;
boost::function<int (const char *,const char *, Request     *request)> RequestFunc; 
boost::function<int (const char *,const char *, Request2 *request)>     RequestFunc2;
class Request
{

};
class Request2
{

};
class BindingProxy
{
    public:
    int  sendRequest(char *,char *, Request * request)
    {
        return 0;
    }
};
class BindingProxy2
{
    public:
    int  sendRequest(char *,char *, Request2 * request)
    {
        return 0;
    }
};
template <typename F>
void wrapperFunc(F func)
{
    Request request;
    func("","",request);
}
int main()
{
  BindingProxy proxy;  

  RequestFunc  = boost::bind(&BindingProxy::sendRequest, &proxy,_1,_2,_3 );

  wrapperFunc(RequestFunc);

  return 0;
}

Solution

  • First, signature of sendRequest doesn't match to signature passed into boost:function, should be:

    int  sendRequest(const char *,const char *, Request * request)
    

    Second, wrapper takes pointer to request, so get it and pass:

    Request request;
    func("","",&request);
    

    Live demo