|
Boost : |
From: Aleksey Gurtovoy (agurtovoy_at_[hidden])
Date: 2002-11-09 04:10:24
A crazy thought - how about enabling something like this:
#include <boost/function.hpp>
#include <cassert>
int main()
{
boost::function<bool ()> f0 = true;
boost::function<int ()> f1 = -1;
boost::function<std::string ()> f2= "text";
assert(f0() == true);
assert(f1() == -1);
assert(f2() == "text");
}
In other words, can we allow a zero-arity 'function<...>' to treat ordinary
values as first-class function objects?
It would make a lot of things so cooler :). For instance:
class widget
{
public:
// ...
typedef boost::function<bool ()> bool_arg_t;
self_t& enable(bool_arg_t a_enable); // note the signature!
bool is_enabled() const;
};
int main()
{
widget w;
w.enable(true); // ordinary syntax/semantics
assert(w.is_enabled())
widget w2;
w.enable(boost::bind(&widget::is_enabled, &w2)); // here!
w2.enable(false);
assert(!w.is_enabled()) // !
}
Pure & beautiful, IMO.
Aleksey
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk