boost::phoenix
C++11 をサポートしていない古いコンパイラで C++ ラムダ式をエミュレートしようとしていますが、ラムダ式内から単純な関数を呼び出すことができません。
C++11 バージョン:
[](unsigned a) { foo( a ); }( 12678u ); // calls foo( 12678u )
私の Phoenix Lambda コードは次のとおりです。
#include <cstdint>
#include <iostream>
#include <boost/phoenix.hpp>
namespace ph = boost::phoenix;
using ph::local_names::_a;
using ph::placeholders::arg1;
void foo( uint32_t val )
{
std::cout << "\t" << __func__ << "( " << val << " ) called...\n";
}
int main()
{
auto myLambda = ph::lambda( _a = arg1 )
[
foo( _a )
//std::cout << ph::val( "Called with: " ) << _a << ph::val( "\n" )
]( 567u );
myLambda();
return 0;
}
これにより、次のコンパイラ エラーが発生します。
lambda-ex.cpp: In function ‘int main()’:
lambda-ex.cpp:18:19: error: cannot convert ‘const _a_type {aka const boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tag::terminal, boost::proto::argsns_::term<boost::phoenix::detail::local<boost::phoenix::local_names::_a_key> >, 0l> >}’ to ‘uint32_t {aka unsigned int}’ for argument ‘1’ to ‘void foo(uint32_t)’ lambda-ex.cpp:20:15: error: unable to deduce ‘auto’ from ‘<expression error>’
Phoenix ラムダ式内から関数を呼び出すにはどうすればよいですか?
phoneix::lambdas
過去に C++11 ラムダを使用したのと同じ方法で使用できることを望んでいます。
auto lambda1 = [&]( uint32_t arg )
{
func1( "Some Stuff", arg );
func2( "Some More Stuff", aValueFromLocalScope, arg );
func3( "Some Stuff", anotherValueFromLocalScope, arg );
};
someFuncImpl( aParam, lambda1 );