I am using boost io_service to run methods asynchronously:
void my_class::completion_handler()
{
...
}
m_io_service.post(boost::bind(&my_class::completion_handler, this));
I would like to use lambda expression instead of boost::bind (see below) in order to avoid creating method for each handler but I am using a C++ compiler that does not support C++11 fully:
m_io_service.post([this](){ ... });
Is it possible to have the same behavior by using phoenix lambda ?
Thank you.
Yes that's possible.
Most notable difference is the placeholders (don't use
std::place_holders::_1,_2... butboost::phoenix::arg_names::arg1,arg2...).However, simply replacing
boost::bindwithstd::bind,boost::lambda::bindorboost::phoenix::bindis ultimately useless of course.Instead you could use Phoenix actors to compose "lambdas", like e.g.
Member invocations are tricky in that respect.
The good news is that Phoenix comes with implementations of many STL operations like
size(),empty(),push_back()etc.Similar use of Phoenix in this queue implementation: Boost group_threads Maximal number of parallel thread and e.g. asio::io_service and thread_group lifecycle issue).
boost::fusion::function<>You can adapt free functions with
BOOST_PHOENIX_ADAPT_FUNCTIONand function objects withBOOST_PHOENIX_ADAPT_CALLABLE. However in the latter case it's probably more elegant to useboost::fusion::function<>: