/*============================================================================= Phoenix V1.2.1 Copyright (c) 2001-2003 Joel de Guzman Use, modification and distribution is subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) ==============================================================================*/ #include #include #include #include #include #include #include using namespace std; using namespace phoenix; ////////////////////////////////// template struct if_else_composite { typedef if_else_composite self_t; template struct result { typedef typename higher_rank< typename actor_result::plain_type, typename actor_result::plain_type >::type type; }; if_else_composite( CondT const& cond_, TrueT const& true__, FalseT const& false__) : cond(cond_), true_(true__), false_(false__) {} template typename actor_result::type eval(TupleT const& args) const { return cond.eval(args) ? true_.eval(args) : false_.eval(args); } CondT cond; TrueT true_; FalseT false_; // actors }; ////////////////////////////////// template inline actor::type, typename as_actor::type, typename as_actor::type> > if_else_(CondT const& cond, TrueT const& true_, FalseT const& false_) { typedef if_else_composite< typename as_actor::type, typename as_actor::type, typename as_actor::type> result; return result( as_actor::convert(cond), as_actor::convert(true_), as_actor::convert(false_)); } ////////////////////////////////// int main() { int init[] = { 2, 10, 4, 5, 1, 6, 8, 3, 9, 7 }; vector c(init, init + 10); typedef vector::iterator iterator; // Print all contents of an stl container c and // prefix " is odd" or " is even" appropriately. for_each(c.begin(), c.end(), cout << arg1 << if_else_(arg1 % 2 == 1, " is odd", " is even") << val('\n') ); return 0; }