| 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/equal.hpp> |
| 7 | #include <boost/hana/integral_constant.hpp> |
| 8 | #include <boost/hana/less.hpp> |
| 9 | #include <boost/hana/plus.hpp> |
| 10 | #include <boost/hana/while.hpp> |
| 11 | |
| 12 | #include <vector> |
| 13 | namespace hana = boost::hana; |
| 14 | using namespace hana::literals; |
| 15 | |
| 16 | |
| 17 | int main() { |
| 18 | // while_ with a Constant condition (loop is unrolled at compile-time) |
| 19 | { |
| 20 | std::vector<int> ints; |
| 21 | auto final_state = hana::while_(hana::less.than(10_c), 0_c, [&](auto i) { |
| 22 | ints.push_back(i); |
| 23 | return i + 1_c; |
| 24 | }); |
| 25 | |
| 26 | // The state is known at compile-time |
| 27 | BOOST_HANA_CONSTANT_CHECK(final_state == 10_c); |
| 28 | |
| 29 | BOOST_HANA_RUNTIME_CHECK(ints == std::vector<int>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); |
| 30 | } |
| 31 | |
| 32 | // while_ with a constexpr or runtime condition (loop is not unrolled) |
| 33 | { |
| 34 | std::vector<int> ints; |
| 35 | int final_state = hana::while_(hana::less.than(10), 0, [&](int i) { |
| 36 | ints.push_back(x: i); |
| 37 | return i + 1; |
| 38 | }); |
| 39 | |
| 40 | // The state is known only at runtime, or at compile-time if constexpr |
| 41 | BOOST_HANA_RUNTIME_CHECK(final_state == 10); |
| 42 | |
| 43 | BOOST_HANA_RUNTIME_CHECK(ints == std::vector<int>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); |
| 44 | } |
| 45 | } |
| 46 | |