Re: boost::bind with references
Zachary Turner <divisortheory <at> gmail.com>
2009-07-01 02:08:07 GMT
On Tue, Jun 30, 2009 at 8:57 PM, Steven Watanabe<watanabesj <at> gmail.com> wrote:
> AMDG
>
> Zachary Turner wrote:
>>
>> I have a function defined as
>>
>> template<typename T>
>> void foo(const T& x);
>>
>> and then I use boost::bind(foo, x);
>>
>> However, once the code is inside the body of foo, the addresses of x
>> inside foo and the addresses of x when I called bind are different.
>> So somehow a copy is being made. I can get around it by using
>> boost::ref(), but is there a technical reason why it doesn't "just
>> work" and pass it by reference
>
> Capturing by reference is more dangerous because
> it can leave dangling references.
>
Well, but what I mean is, shouldn't it pass it exactly according to
how the function is specified? Or maybe there's just no way to do
this. If the function is specified as taking a T& then I would expect
it to pass by reference, whereas if the function is specified as
taking a T, then I would expect it to pass by value. Currently it's
passing by value even if the function is expecting a reference, which
will almost never be the programmer's intention, and can introduce
slicing among other things. Am I missing something?
(Continue reading)