factorial.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*=============================================================================
  2. Copyright (c) 2001-2003 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #include <vector>
  7. #include <algorithm>
  8. #include <iostream>
  9. #include <boost/phoenix/core.hpp>
  10. #include <boost/phoenix/function.hpp>
  11. struct factorial_impl
  12. {
  13. template <typename Sig>
  14. struct result;
  15. template <typename This, typename Arg>
  16. struct result<This(Arg)>
  17. : result<This(Arg const &)>
  18. {};
  19. template <typename This, typename Arg>
  20. struct result<This(Arg &)>
  21. {
  22. typedef Arg type;
  23. };
  24. template <typename Arg>
  25. Arg operator()(Arg n) const
  26. {
  27. return (n <= 0) ? 1 : n * this->operator()(n-1);
  28. }
  29. };
  30. int
  31. main()
  32. {
  33. using boost::phoenix::arg_names::arg1;
  34. boost::phoenix::function<factorial_impl> factorial;
  35. int i = 4;
  36. std::cout << factorial(i)() << std::endl;
  37. std::cout << factorial(arg1)(i) << std::endl;
  38. return 0;
  39. }