|
Boost Users : |
Subject: Re: [Boost-users] [bind] Need Help - Function Composition
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-12-20 21:50:22
AMDG
loadcom wrote:
> I'm not sure if what you mean is that in your use case:
>
> boost::function f = make_less_by_func(boost::bind(...));
>
> the temporarily constructed bind obj will go out of its lifetime before
> we use it via a refence to it?
>
Yes. the temporary is destroyed as soon as the boost::function<...>
constructor runs.
f is then left with a reference to an object that has been destroyed.
> But if I cannot declare
>
> const F& func_;
>
> instead of
>
>
> F func_;
>
> then why bother using another ref lib?
>
You can use a reference in some cases. Using Boost.Ref forces
the caller to be explicit about whether to use a reference so presumably
the user knows that it is safe.
> or, in other words, is there a way to save this 'unneeded' copy
> construction and allow us to use
>
> make_less_by_func(boost::bind(...))
> ?
>
There is no way to avoid the copy while maintaining this interface.
However, it is quite likely that the compiler can optimize away the copy
when you are only using Boost.Bind with built in types.
> btw, I'm not using ref lib frequently, but should boost::unwrap_ref
> be boost::unwrap_reference?
>
No. boost::unwrap_reference is a metafunction.
In Christ,
Steven Watanabe
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net