Boost logo

Boost Users :

Subject: [Boost-users] [lambda] How to write a lambda functor that returns a new functor
From: Sebastian Theophil (stheophil_at_[hidden])
Date: 2011-03-09 09:11:24


Hi,

How can I write a lambda expression with two placeholders, one for the
callable object, and one for the function argument, such that supplying
the callable object first returns a unary function?

In the example below, generate should be a lambda expression with the
first placeholder for the callable object itself, and the second
placeholder for the argument. Calling generate(c) should return a unary
function that is only missing the function call argument. In fact, it
somehow returns type bool already, as proved by the static assert.

#include <boost/lambda/bind.hpp>

struct Arg {
};

struct Callable : std::unary_function<Arg, bool> {
    bool operator()( Arg const& a ) const { return true; }
};

int main( int argc, const char* argv[] ) {
    BOOST_AUTO(generate, boost::lambda::bind(boost::lambda::_1,
boost::lambda::protect(boost::lambda::_1)));

    Callable c;
    BOOST_AUTO(fn, generate(c));

    BOOST_STATIC_ASSERT((boost::is_same<BOOST_TYPEOF(fn),
bool>::value));
    Arg a;
    bool b = fn(a);
    _ASSERT(b==true);
}

 

Thanks,

Sebastian

 

--
Sebastian Theophil | stheophil_at_[hidden]
Senior Software Engineer
think-cell Software GmbH | Chausseestr. 8/E | 10115 Berlin | Germany
http://www.think-cell.com | phone +49 30 666473-10 | US phone +1 800 891 8091
Amtsgericht Berlin-Charlottenburg, HRB 85229 | European Union VAT Id DE813474306
Directors: Dr. Markus Hannebauer, Dr. Arno Schoedl


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