ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Setting up your robot using tf - boost::bind()

asked 2016-04-06 10:53:57 -0600

Randerson gravatar image

Hello all, I am passing just a hard time trying to understand ros, c++ and associated concepts. Probably, here is not the best place to post the question, but I will glad with some help me with my doubts.

Here we go. From the Setting up your robot using tf Navigation Stack tutoriais we have the following snippet code:

  ros::Timer timer = n.createTimer(ros::Duration(1.0),
                               boost::bind(&transformPoint,
                                           boost::ref(listener)));

I am not sure that I really understand what is going here. For me, it seems that in every second the method boost::bind will be called as a callback function. This method, when called, will call the function transformPoint passing to it the argument listener in terms of boos::ref(). In general, I suppose that this is what is happening but I thinking that I am missing something. So, I have some questions.

1_ Why it is used boost::ref to pass the listener variable as argument? Could I pass it directly, i.e. without the method boost::ref?

As a exercise, I tried to reproduce the same behavior in the following code:

#include <iostream>
#include "boost/bind.hpp"
#include "boost/ref.hpp"

void show(char a){
    std::cout << a << std::endl;
}

int main() {    
    char letter = 'a';
    boost::bind(&show, boost::ref(letter));
}

But, It did not work as I as expecting. The function show was not called.

2_ It is possible to reproduce the same behavior presented by the snippet code above? If so, how?

I could reproduce a similar behavior with:

int main() {    
    char letter = 'a';
    boost::bind(&show, _1)(boost::ref(letter));
}

But, it is in the same format that it is used in the ros implementation.

Thanks in advance, any help is welcome.

ps. I already look for exemples in internet, but I could not find exactly the answer to my doubts.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-04-06 12:42:20 -0600

ahendrix gravatar image

boost::bind() returns something that can be called like a function. It doesn't actually call the function.

In your first example, you should be able to reproduce the behavior with:

boost::bind(&show, boost::ref(letter))();

Or if you want it to call that function more than once (like the TF example does), you could do:

boost::function f = boost::bind(&show, boost::ref(letter));
f();
f();
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2016-04-06 10:53:57 -0600

Seen: 490 times

Last updated: Apr 06 '16