|
Boost : |
From: David Abrahams (dave_at_[hidden])
Date: 2005-11-03 15:57:10
Joel de Guzman <joel_at_[hidden]> writes:
> Comments, feedback very welcome!
Lazy Functions
As you write more lambda functions, you'll notice certain patterns
that you wish to refactor as reusable functions. When you reach that
point, you'll wish that ordinary functions can co-exist with phoenix
functions. Unfortunately, the immediate nature of plain C++
functions make them incompatible.
Lazy functions are your friends. The library provides a facility to
make lazy functions. The code below is a rewrite of the is_odd
function using the facility:
struct is_odd_impl
{
template <typename Arg>
struct apply
{
typedef bool type;
};
template <typename Arg>
bool operator()(Arg arg1) const
{
return arg1 % 2 == 1;
}
};
I kept looking for where the library facility was in use... and then I
saw this:
function<is_odd_impl> is_odd;
^^^^^^^^
I guess that's it. It would help to leave a phoenix:: qualification
on names from the library, or if you can't do that, use color or bold
to emphasize them.
Y'know, what I really expected, though, was something I could pass the
old is_odd function to:
make_function(is_odd)
-- Dave Abrahams Boost Consulting www.boost-consulting.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk