[lambda] Return types in lambda expressions

I'm trying to use bind and lambda to create a functor that can receive an event generated by my GUI library, and dispatch an appropriate event into a boost::statechart::state_machine. The GUI library requires the functor to have the signature bool (const CEGUI::EventArgs&). An example of what works: template<typename EventType> bool GuiEvent(const CEGUI::EventArgs&, GameStateMachine& gsm) { gsm.process_event(EventType( )); return true; } //usage: GameStateMachine sm; ... boost::bind(&GuiEvent<EventExitGame>, _1, boost::ref(sm)); But is it possible to use lambda to eliminate the need for the function? It looks like I would use something along the lines of boost::bind<bool>(boost::ref(sm).process_event(EventExitGame( )) /*somehow return true here*/, _1); I'm just not quite seeing how I would get the lambda expression to return true there. Cheers.

"Michael Crawford" <mbcrawfo@gmail.com> wrote in message news:9f5be0740904260914sf375ddnfef0f1bb8bb62707@mail.gmail.com...
I'm trying to use bind and lambda to create a functor that can receive an event generated by my GUI library, and dispatch an appropriate event into a
boost::statechart::state_machine. The GUI library requires the functor to have the signature bool (const CEGUI::EventArgs&). An example of what works: template<typename EventType> bool GuiEvent(const CEGUI::EventArgs&, GameStateMachine& gsm) { gsm.process_event(EventType( )); return true; }
//usage: GameStateMachine sm; ... boost::bind(&GuiEvent<EventExitGame>, _1, boost::ref(sm)); But is it possible to use lambda to eliminate the need for the function? It looks like I would use something along the lines of boost::bind<bool>(boost::ref(sm).process_event(EventExitGame( )) /*somehow return true here*/, _1);
As you don't provide the call signature of the callback registration method (which should be there somewhere) this is probably not entirely correct, but something like this should be possible (beware of typos): void foo_add_callback(::boost::function0<bool> const& cb) { ... } #include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> ... namespace lambda = ::boost::lambda; ... foo_add_callback(( lambda::bind(&GameStateMachine::process_event<EventExitGame>, lambda::var(sm), EventExitGame()), true )); (note extra parens and the usage of the comma operator) / Johan
participants (2)
-
Johan Nilsson
-
Michael Crawford