简体   繁体   English

如何使用boost :: bind正确绑定成员函数

[英]How to correctly bind a member function with boost::bind

I am struggling to use boost::function and boost::bind correctly to bind the passed object and member function into a boost::function which than is later called when data are in the queue 我正在努力使用boost :: function和boost :: bind来正确地将传递的对象和成员函数绑定到boost :: function中,然后当数据在队列中时会被调用

I am working on a multi producer single consumer queue for inter-thread communication. 我正在为线程间通信的多生产者单一使用者队列工作。 The basic idea is to provide a template class. 基本思想是提供一个模板类。 When data published into the queue, the subscriber gets the data via a callback function which has to supply on subscription. 当数据发布到队列中时,订阅者通过必须提供订阅的回调函数来获取数据。

Note: ... denotes places where I omit code for legibility reasons 注意: ...表示出于可读性原因我忽略代码的地方

T is the type which is stored in the queue T是存储在队列中的类型

template <typename T>
class pipeline_mpsc
{...};

The subscribe function 订阅功能

  template <typename FUNC, class OBJ>
  bool subscribe(FUNC fn, OBJ obj)
  {
    bool loc_return = false;
    if (Callback_fn_ == NULL)
    {

      boost::function<void(const T &)> BoundCallback(boost::bind(fn, obj, _1));

      Callback_fn_ = &BoundCallback;
      boost::thread th(&pipeline_mpsc::Callbackcaller, this, &BoundCallback); //pipeline.hpp[38, 25]
      loc_return = true;
    }
    return loc_return;
  };
void Callbackcaller(boost::function<void(const T &)> *Callback_fn)  {
   ...
   Callback_fn(loc_tmp); //pipeline.hpp[96, 18]
};

How I call the subscribe function 我如何调用订阅功能

laserscan_sub_->subscribe(&LidarFovFilter::laserscan_callback, this); //LidarFovFilter.cpp[25, 73]

Prototype of the Callback function 回调函数的原型

void LidarFovFilter::laserscan_callback(const LaserScan &scan)

When I compile it I get following error from gcc: 编译时,我从gcc收到以下错误:

'Callback_fn' cannot be used as a function 'Callback_fn'不能用作函数

pipeline.hpp[96, 18]:In instantiation of 'void pipeline_mpsc::Callbackcaller(boost::function ) [with T = LaserScan]': pipeline.hpp [ 96,18] :在“空pipeline_mpsc :: Callbackcaller(boost :: function )[带有T = LaserScan]的实例中”:

pipeline.hpp[38, 25]:required from 'bool pipeline_mpsc::subscribe(FUNC, OBJ) [with FUNC = void (LidarFovFilter:: )(const LaserScan&); pipeline.hpp [38,25]:从'boolpipeline_mpsc :: subscribe(FUNC,OBJ)中获得[FUNC = void(LidarFovFilter :: )(const LaserScan&); OBJ = LidarFovFilter*; OBJ = LidarFovFilter *; T = LaserScan]' T = LaserScan]'

LidarFovFilter.cpp[25, 73]: required from here LidarFovFilter.cpp [25,73]:从这里开始需要

From what I read how to use boost::bind and boost::function I think it my code should work (but obviously it doesn't).I am at loss here why it doesn't. 从我阅读的如何使用boost :: bind和boost :: function的角度来看,我认为它应该可以在我的代码中正常工作(但显然不行)。我不知道为什么不行。 Where is my mistake? 我的错误在哪里? Help would be really appricated. 帮助将是真的。

Issue is you pass a pointer to a boost::function - you need to dereference first: 问题是您传递了一个指向boost :: function的指针-您需要先解除引用:

void Callbackcaller(boost::function<void(const T &)> *Callback_fn)  {
   ...
   (*Callback_fn)(loc_tmp); //pipeline.hpp[96, 18]
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM