phoenix_factorial.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright (C) 2009-2012 Lorenzo Caminiti
  2. // Distributed under the Boost Software License, Version 1.0
  3. // (see accompanying file LICENSE_1_0.txt or a copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Home at http://www.boost.org/libs/local_function
  6. #include <boost/phoenix/core.hpp>
  7. #include <boost/phoenix/function.hpp>
  8. #include <boost/detail/lightweight_test.hpp>
  9. //[phoenix_factorial
  10. struct factorial_impl { // Phoenix function from global functor.
  11. template<typename Sig>
  12. struct result;
  13. template<typename This, typename Arg>
  14. struct result<This (Arg)> : result<This (Arg const&)> {};
  15. template<typename This, typename Arg>
  16. struct result<This (Arg&)> { typedef Arg type; };
  17. template<typename Arg> // Polymorphic.
  18. Arg operator()(Arg n) const {
  19. return (n <= 0) ? 1 : n * (*this)(n - 1);
  20. }
  21. };
  22. int main(void) {
  23. using boost::phoenix::arg_names::arg1;
  24. boost::phoenix::function<factorial_impl> factorial;
  25. int i = 4;
  26. BOOST_TEST(factorial(i)() == 24); // Call.
  27. BOOST_TEST(factorial(arg1)(i) == 24); // Lazy call.
  28. return boost::report_errors();
  29. }
  30. //]