repeat.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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/fwd/concept/integral_constant.hpp>
  6. #include <boost/hana/repeat.hpp>
  7. namespace hana = boost::hana;
  8. //////////////////////////////////////////////////////////////////////////////
  9. // Define a simple model of IntegralConstant for use below
  10. template <int i>
  11. struct constant {
  12. static constexpr int value = i;
  13. using value_type = int;
  14. };
  15. namespace boost { namespace hana {
  16. template <int i>
  17. struct IntegralConstant<constant<i>> {
  18. static constexpr bool value = true;
  19. };
  20. // definition of `to<>` omitted
  21. }}
  22. //////////////////////////////////////////////////////////////////////////////
  23. void function() { }
  24. int main() {
  25. int counter = 0;
  26. hana::repeat(constant<3>{}, [&] { ++counter; });
  27. BOOST_HANA_RUNTIME_CHECK(counter == 3);
  28. // Try with a normal function.
  29. hana::repeat(constant<3>{}, function);
  30. // Try with a function pointer.
  31. hana::repeat(constant<3>{}, static_cast<void(*)()>(function));
  32. // Make sure we don't read from a non-constexpr variable.
  33. constant<3> three{};
  34. hana::repeat(three, []{});
  35. }