fix.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright Louis Dionne 2013-2017
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  4. #include <boost/hana/functional/fix.hpp>
  5. #include <boost/hana/assert.hpp>
  6. #include <boost/hana/config.hpp>
  7. #include <boost/hana/equal.hpp>
  8. #include <boost/hana/eval_if.hpp>
  9. #include <boost/hana/functional/always.hpp>
  10. #include <boost/hana/integral_constant.hpp>
  11. #include <boost/hana/minus.hpp>
  12. #include <boost/hana/mult.hpp>
  13. namespace hana = boost::hana;
  14. BOOST_HANA_CONSTEXPR_LAMBDA auto fact = hana::fix([](auto fact, auto n) {
  15. return hana::eval_if(hana::equal(n, hana::ullong_c<0>),
  16. hana::always(hana::ullong_c<1>),
  17. [=](auto _) { return hana::mult(n, fact(_(n) - hana::ullong_c<1>)); }
  18. );
  19. });
  20. constexpr unsigned long long reference(unsigned long long n)
  21. { return n == 0 ? 1 : n * reference(n - 1); }
  22. template <int n>
  23. void test() {
  24. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  25. fact(hana::ullong_c<n>),
  26. hana::ullong_c<reference(n)>
  27. ));
  28. test<n - 1>();
  29. }
  30. template <> void test<-1>() { }
  31. int main() {
  32. test<15>();
  33. }