Unable to call lazy lambda function with parameters via boost :: phoenix :: function

I tried calling boost::phoenix::function based on lambda function with parameters and failed. If I call it without parameters like this:

 const auto closure = [](){ cout<< "test" << endl; }; typedef decltype(closure) ClosureType; const boost::phoenix::function<ClosureType> lazyFunc (std::move(closure)); lazyFunc()(); 

everything compiles. But when I declare at least one lambda parameter:

 const auto closure = [](int& param) { cout<<"Test" << param << endl; }; typedef decltype(closure) ClosureType; const boost::phoenix::function<ClosureType> lazyFunc (std::move(closure)); lazyFunc(arg1)(a); 

compilation fails with huge stack trace inside boost :: result_of

+2
source share
1 answer

Assuming the error points somewhere deep inside Boost.ResultOf (as shown in this demo ), this would be because the closure type of the lambda expression does not implement the ResultOf protocol.

A somewhat simple workaround is to define BOOST_RESULT_OF_USE_DECLTYPE , which makes boost::result_of bypass its own ResultOf protocol, instead using decltype to evaluate return types. This is not enabled by default, because not many compilers (during the release of Boost 1.51) are compatible enough to make this function work; it is planned that this symbol will be determined automatically (by Boost.Config) for those compilers that can handle it in 1.52.

Here is a demonstration of how it looks like Boost.Phoenix with decltype -powered boost::result_of . I had to change int& to int const& , because i apparently being forwarded as const int . This seems to be the fundamental limitation of boost::phoenix::function , using boost::phoenix::val does not have this problem.

+2
source

Source: https://habr.com/ru/post/1401446/


All Articles