Boost logo

Boost Users :

Subject: Re: [Boost-users] [bind] Need Help - Function Composition
From: loadcom (loadcom_at_[hidden])
Date: 2008-12-20 20:04:14


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_at_[hidden]
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/ )


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