使用派生函数参数绑定

时间:2014-08-06 21:10:14

标签: c++ std-function stdbind

是否可以使用派生参数绑定函数?如果怎么样?我希望能够将函数点存储到各种具有相似签名的函数,即它们使用输入数据的类并返回具有输出值的类。但是不同的函数需要并提供不同的参数,因此我试图注册采用派生消息类的函数。

以下代码部分有效。我可以注册MathService :: blank函数,稍后我可以调用它。但是我无法添加MathService :: add。

我得到的错误是:

  

main.cpp:70:93:错误:从'std :: _ Bind_helper&)转换(RequestMessage&,ReplyMessage&),MathService&,const std :: _ Placeholder< 1>&,const std :: _ Placeholder< 2>&> :: type {aka std :: _ Bind(MathService *,std :: _ Placeholder< 1>,std :: _ Placeholder< 2>)>}'到非标量类型'Service :: serviceFunction_t { aka std :: function}'已请求          serviceFunction_t fn = bind(methodPtr,objectPtr,placeholders :: _ 1,placeholders :: _ 2);

#include <functional>
#include <unordered_map>
#include <iostream>

using namespace std;

// base class for messages passed around
class BaseMessage
{
  public:
    virtual void print()
    {
      cout << "BaseMessage\n";
    }
};

// request message with data passed to the service
class RequestMessage : public BaseMessage
{
  public:
    RequestMessage( int a_, int b_ ) : a( a_ ), b( b_ ) {}
    int a;
    int b;
    void print()
    {
      cout << "RequestMessage a=" << a << " b=" << b << endl;
    }
};

// reply message with return values from the service
class ReplyMessage : public BaseMessage
{
  public:
    ReplyMessage() : sum( 0 ) {}
    int sum;
    void print()
    {
      cout << "ReplyMessage sum=" << sum << endl;
    }
};

// Example service provider
class MathService
{
  public:

    void blank( BaseMessage& request, BaseMessage& reply )
    {
      request.print();
      reply.print();
    }

    void add( RequestMessage& request, ReplyMessage& reply )
    {
      reply.sum = request.a + request.b;
    }
};


// Class manages services, register a new service with addService and call the service by name using call
class Service
{
  public:
    using serviceFunction_t = function<void ( BaseMessage&, BaseMessage& )>;

    template<class Method, class Obj>
    void addService( string name,  Method methodPtr, Obj objectPtr )
    {
      serviceFunction_t fn = bind( methodPtr, objectPtr, placeholders::_1, placeholders::_2 );

      pair< string, serviceFunction_t> entry( name, fn );
      mFunctionMap.insert( entry );
    }


    void call( const string& name, BaseMessage& request, BaseMessage& reply )
    {
      std::unordered_map<string, serviceFunction_t>::const_iterator it;
      it = mFunctionMap.find( name );

      if( it == mFunctionMap.end() ) {
        std::cout << "service not found: " << name << endl;
        return;
      }

      serviceFunction_t fn =  it->second;
      fn( request, reply );
    }

  private:
    unordered_map<string, serviceFunction_t> mFunctionMap;
};


int main()
{
  MathService math;
  Service service;

  // can add a service with BaseMessages
  service.addService( "blank", &MathService::blank, &math );


  //*****************************************************
  // PROBLEM is here !!
  // can not add a service with derived message types, this causes the bind call to fail in Service::addService()

  service.addService( "add", &MathService::add, &math );

  //*****************************************************

  // this works
  BaseMessage req1, rep1;
  service.call( "blank", req1, rep1 );

  // so does this
  RequestMessage req2( 1, 2 );
  ReplyMessage rep2;

  service.call( "blank", req2, rep2 );

  // this service is not registered
  service.call( "add", req2, rep2 );

}

1 个答案:

答案 0 :(得分:0)

1) do是一个保留的C ++关键字,因此您的代码将永远不会编译

2) std::placeholders_1不存在,您的确意味着std::placeholders::_1

3)一旦修复,是的,it compiles