Reputation: 563
I've been studying boost::bind and boost::function. I can best explain my understanding by giving an example.. Given this sample code snippet:
void print(int x, int y){
cout << x << "\t" << y << endl;
}
int main(){
boost::function<void (int)> f = boost::bind (&print, _1, 2);
f(5);
}
Displays 5 2.. From my understanding, binding a function creates a function object that can have some of its arguments bound to some constant arguments (programmer's preference).
However, what I really can't understand is this code snippet of the source code posted below:
boost::function<void (const pcl::PointCloud<pcl::PointXYZ>::ConstPtr&)> f =
boost::bind (&SimpleOpenNIViewer::cloud_cb_, this, _1);
interface->registerCallback (f);
The argument placer is _1. Should not it be f(arg)? Why is the arg omitted?
#include <pcl/io/openni_grabber.h>
#include <pcl/visualization/cloud_viewer.h>
class SimpleOpenNIViewer
{
public:
SimpleOpenNIViewer () : viewer ("PCL OpenNI Viewer") {}
void cloud_cb_ (const pcl::PointCloud<pcl::PointXYZ>::ConstPtr &cloud)
{
if (!viewer.wasStopped())
viewer.showCloud (cloud);
}
void run ()
{
pcl::Grabber* interface = new pcl::OpenNIGrabber();
boost::function<void (const pcl::PointCloud<pcl::PointXYZ>::ConstPtr&)> f =
boost::bind (&SimpleOpenNIViewer::cloud_cb_, this, _1);
interface->registerCallback (f);
interface->start ();
while (!viewer.wasStopped())
{
boost::this_thread::sleep (boost::posix_time::seconds (1));
}
interface->stop ();
}
pcl::visualization::CloudViewer viewer;
};
int main ()
{
SimpleOpenNIViewer v;
v.run ();
return 0;
}
Upvotes: 0
Views: 112
Reputation: 96790
No, the function is not attempting to be called on registerCallback(f)
. The function f
is being passed to the parameter that accepts a boost::function
. The arguments will eventually be given to f
when it is called at some later point. For example:
typedef boost::function<void (int)> Function;
void h( Function f )
{
f(5);
}
int main()
{
auto cube = [] (int n) { std::cout << n * n * n; };
h(cube); // cube is passed to the function, not called
}
Upvotes: 2