times.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/assert.hpp>
  5. #include <boost/hana/core/is_a.hpp>
  6. #include <boost/hana/integral_constant.hpp>
  7. #include <boost/hana/value.hpp>
  8. namespace hana = boost::hana;
  9. void function() { }
  10. void function_index(...) { }
  11. int main() {
  12. // times member function
  13. {
  14. int counter = 0;
  15. hana::int_c<3>.times([&] { ++counter; });
  16. BOOST_HANA_RUNTIME_CHECK(counter == 3);
  17. // Call .times with a normal function used to fail.
  18. hana::int_c<3>.times(function);
  19. // make sure times can be accessed as a static member function too
  20. decltype(hana::int_c<5>)::times([]{ });
  21. // make sure xxx.times can be used as a function object
  22. auto z = hana::int_c<5>.times;
  23. (void)z;
  24. }
  25. // times.with_index
  26. {
  27. int index = 0;
  28. hana::int_c<3>.times.with_index([&](auto i) {
  29. static_assert(hana::is_an<hana::integral_constant_tag<int>>(i), "");
  30. BOOST_HANA_RUNTIME_CHECK(hana::value(i) == index);
  31. ++index;
  32. });
  33. // Calling .times.with_index with a normal function used to fail.
  34. hana::int_c<3>.times.with_index(function_index);
  35. // make sure times.with_index can be accessed as a static member
  36. // function too
  37. auto times = hana::int_c<5>.times;
  38. decltype(times)::with_index([](auto) { });
  39. // make sure xxx.times.with_index can be used as a function object
  40. auto z = hana::int_c<5>.times.with_index;
  41. (void)z;
  42. // make sure we're calling the function in the right order
  43. int current = 0;
  44. hana::int_c<5>.times.with_index([&](auto i) {
  45. BOOST_HANA_RUNTIME_CHECK(hana::value(i) == current);
  46. ++current;
  47. });
  48. }
  49. }