I ran into similar issue expecting a bind parameter to be passed by reference whenever the method used in the bind was declared to take a reference parameter. However this is NOT the case! You will need to explicitly wrap the bind parameter (that is to be passed by reference) in a boost::ref() or boost::cref() regardless of how the method is declared.
Example:
ClassA myClassAParameter
void Method(ClassA ¶m);
now, the following binding:
callback = boost::bind(&Method, myClassAParameter);
will actually make a COPY of the ClassA object (which i understand it is a temporary allocation and the called method should not keep a reference to it since this is not the reference of the actual object but to a copy of the object).
however, the following binding:
callback = boost::bind(&Method, boost::ref(myClassAParameter));
will not make a copy, but use a reference to create the bind object.
void increment_int(int& x)
if you accidentally forget to useboost::ref
when you callint tmp = 0
,boost::function<void()> func = boost::bind(f, tmp)
--> when you execute the function,tmp
will not be incremented. Instead the object created byboost::bind
saves a copy and calls the functionincrement_int
with the copy. for more info see here. I also made sure to create a short, self contained, compilable example to test/verify what I said. – Gilles