ROS与boost::bind( )

来源:互联网 发布:海辉软件 国际 集团 编辑:程序博客网 时间:2024/06/16 07:43

转自:http://blog.csdn.net/yaked/article/details/44942773

C++的书我自认为看过不少,但是对boost不太熟悉。百度过后boost可以看作是C++的第三方库,像我们常用的iostream等都是自带的库。

因为ROS 的原因,看到许多地方的回调函数(call back)都用到了boost::bind。

1. http://wiki.ros.org/ROSNodeTutorialC%2B%2B

  其中talk.cpp的21行  cb = boost::bind(&NodeExample::configCallback, node_example, _1, _2);

其中的 node_example是指针变量NodeExample *node_example = new NodeExample();

2. http://wiki.ros.org/actionlib_tutorials/Tutorials/Writing%20a%20Callback%20Based%20Simple%20Action%20Client

  最下面的 fibonacci_class_client.cpp 中的25行

ac.sendGoal(goal,  boost::bind(&MyNode::doneCb, this, _1, _2),  Client::SimpleActiveCallback(),  Client::SimpleFeedbackCallback());

来取代

ac.sendGoal(goal, &doneCb, &activeCb, &feedbackCb);


参考网站:

http://www.boost.org/doc/libs/1_43_0/libs/bind/bind.html#with_member_pointers

bind(f, _1, 5)(x)is equivalent to f(x, 5); here _1 is a placeholder argument that means "substitute with the first input argument."

这里的 _1 相当于一个占位符,用来代替第一个输入参数。

bind( f, 5, _1)(x);                     // f(5, x)

bind( f, _2, _1)(x, y);                 // f(y, x)

bind( g, _1, 9, _1)(x);                 // g(x, 9, x)

bind( g, _3, _3, _3)(x, y, z);          // g(z, z, z)

bind( g, _1, _1, _1)(x, y, z);          // g(x, x, x)
观察上面的两个地方的boost::bind,都是boost::bind(&NodeExample::configCallback, node_example, _1, _2)差不多形式,搞懂一个就好。

bind(&X::f, &x, _1)(i);            //(&x)->f(i)

因此boost::bind(&NodeExample::configCallback, node_example, _1, _2)的意思就是 node_example -> configCallback(x, y)

而另外的那个boost::bind(&MyNode::doneCb, this, _1, _2)意思就是this -> doneCb(x, y) 这里的x,y 分别为第一个和第二个输入参数
0 0