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



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?

share|improve this question
 

2 Answers

up vote28down voteaccepted

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

share|improve this answer
 

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.


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?

share|improve this question
 

2 Answers

up vote28down voteaccepted

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

share|improve this answer
 

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.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章