Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::bind with functions that have parameters that are references

Tags:

c++

boost-bind

I noticed that when passing reference parameters to boost bind, those parameters won't act like references. Instead boost creates another copy of the member and the original passed in variable remains unchanged.

When I change the references to pointers, everything works ok.

My question is:

Is it possible to get references to work, or at least give a compiling error when it tries to use reference parameters?

like image 566
Brian R. Bondy Avatar asked Jan 22 '09 17:01

Brian R. Bondy


People also ask

How do you pass parameters by reference?

Pass-by-reference means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the argument by using its reference passed in. The following example shows how arguments are passed by reference.

How can you pass parameters to a function by call by reference?

The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.

What is boost :: bind?

boost::bind is a generalization of the standard functions std::bind1st and std::bind2nd. It supports arbitrary function objects, functions, function pointers, and member function pointers, and is able to bind any argument to a specific value or route input arguments into arbitrary positions.

Why would you pass parameters to a function by reference?

In pass by reference, no new copy of the variable is made, so overhead of copying is saved. This makes programs efficient especially when passing objects of large structs or classes.


2 Answers

The boost documentation for bind suggests that you can use boost::ref and boost::cref for this.

like image 140
Timo Geusch Avatar answered Oct 02 '22 20:10

Timo Geusch


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 &param);

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.

like image 42
DolphinDream Avatar answered Oct 02 '22 19:10

DolphinDream