/*============================================================================= Copyright (c) 2001-2007 Joel de Guzman Distributed under 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 #define BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS #include #undef BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS struct test_impl { typedef void result_type; void operator()() const { std::cout << "Test lazy functions...\n"; } }; boost::phoenix::function test; struct sqr_impl { template struct result; template struct result { typedef typename boost::remove_reference::type type; }; template Arg operator()(Arg n) const { return n * n; } }; boost::phoenix::function sqr; struct fact_impl { template struct result; template struct result { typedef typename boost::remove_reference::type type; }; template Arg operator()(Arg n) const { return (n <= 0) ? 1 : n * (*this)(n-1); } }; boost::phoenix::function fact; struct pow_impl { template struct result; template struct result { typedef typename boost::remove_reference::type type; }; template Arg1 operator()(Arg1 a, Arg2 b) const { return std::pow(a, b); } }; boost::phoenix::function power; struct add_impl { template struct result; template struct result { typedef typename boost::remove_reference::type type; }; template Arg1 operator()(Arg1 a, Arg2 b, Arg3 c, Arg4 d) const { return a + b + c + d; } }; boost::phoenix::function add; int main() { using boost::phoenix::arg_names::arg1; using boost::phoenix::arg_names::arg2; int i5 = 5; double d5 = 5, d3 = 3; test()(); BOOST_TEST(sqr(arg1)(i5) == (i5*i5)); BOOST_TEST(fact(4)() == 24); BOOST_TEST(fact(arg1)(i5) == 120); BOOST_TEST((int)power(arg1, arg2)(d5, d3) == (int)std::pow(d5, d3)); BOOST_TEST((sqr(arg1) + 5)(i5) == ((i5*i5)+5)); BOOST_TEST(add(arg1, arg1, arg1, arg1)(i5) == (5+5+5+5)); // testing composition BOOST_TEST(add(arg1, arg1, arg1, power(arg1, 2))(d5) == (5+5+5+25)); int const ic5 = 5; // testing consts BOOST_TEST(sqr(arg1)(ic5) == (ic5*ic5)); return boost::report_errors(); }