Main Contents

boost::bind and @#!$ references…

Tags: ,

I’m using boost::bind and boost::thread together and as I’m a kind of guru (or maybe just a kind of jerk…), I never read the documentation (that’s for newbies bla bla…).

Here is my problem today : I’m using boost::bind to pass some arguments to my threaded function. One of these arguments is a reference but doesn’t act like a reference. If you compare the adress of this “reference” before boost::bind and in the function called by boost::bind, it’s not the same. If you replace the reference with a pointer, it works!
At this moment, I thought : “boost::bind doesn’t accept references, boost programmers are @#!$”.

Then, I asked a non-guru friend (ie. someone that often reads the manual…) if he has already noticed such a problem. His response was clear: “Read the fucking manual, moron” :

The arguments that bind takes are copied and held internally by the returned function object. For example, in the following code:

int i = 5;

bind(f, i, _1);

a copy of the value of i is stored into the function object. boost::ref and boost::cref can be used to make the function object store a reference to an object, rather than a copy:

int i = 5;

bind(f, ref(i), _1);

bind(f, cref(42), _1);

If you are on this page, you’re probably a kind of guru too. I have an advice for you: RTFM moron…

romain @ April 3, 2009

Leave a comment


Feed