0

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

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

我得到的错误是:

main.cpp:70:93: 错误:从 'std::_Bind_helper&)(RequestMessage&, ReplyMessage&), MathService&, const std::_Placeholder<1>&, const std::_Placeholder<2>&>::type {又名 std::_Bind(MathService*, std::_Placeholder<1>, std::_Placeholder<2>)>}' 到非标量类型 'Service::serviceFunction_t {aka std::function}' 请求 serviceFunction_t fn =绑定(方法指针,对象指针,占位符::_1,占位符::_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 );

}

4

1 回答 1

0

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

2) std::placeholders_1不存在,你肯定是说std::placeholders::_1

3)一旦修复,是的,它编译

于 2014-08-06T21:23:43.683 回答