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