#include "boost/spirit/phoenix/functions.hpp" #include "boost/spirit/phoenix/operators.hpp" #include "boost/spirit/phoenix/primitives.hpp" #include "boost/spirit/phoenix/statements.hpp" #include "boost/function.hpp" namespace phoenix { template struct placeholder_types; template struct placeholder_types { typedef RT result_type; typedef tuple<> args_type; typedef boost::function function_type; static args_type const& convert_args(args_type const& args) { return args; } template static args_type convert_args(TupleT const& args) { return args_type(); } }; template struct placeholder_types { typedef RT result_type; typedef tuple args_type; typedef boost::function function_type; static args_type const& convert_args(args_type const& args) { return args; } template static args_type convert_args(TupleT const& args) { return args_type(args[tuple_index<0>()]); } }; template struct placeholder_types { typedef RT result_type; typedef tuple args_type; typedef boost::function function_type; static args_type const& convert_args(args_type const& args) { return args; } template static args_type convert_args(TupleT const& args) { return args_type(args[tuple_index<0>()], args[tuple_index<1>()]); } }; template struct placeholder_eval { template struct result { typedef typename placeholder_types::result_type type; }; DerivedT& derived() { return static_cast(*this); } DerivedT const& derived() const { return static_cast(*this); } template typename actor_result::type eval(TupleT const& args) const { return this->derived().f( placeholder_types::convert_args(args)); } }; template struct placeholder_ftor { placeholder_ftor(ActorT const& actor_) : actor(actor_) {} template typename actor_result::type operator()(TupleT const& args) const { return actor.eval(args); } ActorT actor; }; template struct placeholder : placeholder_eval, SigT> { template placeholder(RHS const& rhs) : f(placeholder_ftor(rhs)) {} placeholder(placeholder const& rhs) : f(rhs.f) {} template placeholder& operator=(RHS const& rhs) { f = placeholder_ftor(rhs); } placeholder& operator=(placeholder const& rhs) { f = rhs.f; } typedef actor > type; typename placeholder_types::function_type f; }; } // namespace phoenix #include using namespace std; using namespace phoenix; int main() { placeholder::type inc = arg1 + 1; placeholder::type sqr = arg1 * arg1; placeholder::type add = arg1 + arg2; int i = 3; int j = 4; cout << inc(i) << endl; // result = 4 cout << (inc + inc + sqr)(i) << endl; // result = 17 cout << (inc + add)(i, j) << endl; // result = 11 return 0; }