Re: [Boost-users] [bind] Need Help - Function Composition

Hello Watanabe, 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? But if I cannot declare const F& func_; instead of F func_; then why bother using another ref lib? 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(...)) ? btw, I'm not using ref lib frequently, but should boost::unwrap_ref be boost::unwrap_reference? Thank you so much for your continuous help and patience to my probable stupid questions. Best regards Max ----- Original Message ----- From: Steven Watanabe To: boost-users@lists.boost.org Subject: Re: [Boost-users] [bind] Need Help - Function Composition Date: 2008-12-21 02:39:38 AMDG loadcom wrote:
One step further, will the following version save one copy construction
in make_less_by_func()?
template struct less_by_func
{
typedef bool result_type;
const F & func_;
less_by_func(const F & func) : func_(func) {}
template
bool operator()(const T& lhs, const T& rhs) const
{ return func_(lhs) };
// helper function
template less_by_func make_less_by_func(const F& func)
{ return less_by_func(func); }
I don't advise this in general because it requires the argument of make_less_by_func to survive for a while. boost::function f = make_less_by_func(boost::bind(...)); f(1, 2); // undefined behavior because of the dangling reference. If you really want this capability #include template struct less_by_func { typedef bool result_type; F func_; less_by_func(const F & func) : func_(func) {} template bool operator()(const T& lhs, const T& rhs) const { return boost::unwrap_ref(func_)(lhs) http://lists.boost.org/mailman/listinfo.cgi/boost-users ------------------------------------------------------------------- 新浪空间——与朋友开心分享网络新生活!(http://space.sina.com.cn/ )

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
participants (2)
-
loadcom
-
Steven Watanabe