From 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 20:24:20 +0200 Subject: Adding upstream version 14.2.21. Signed-off-by: Daniel Baumann --- src/boost/libs/hana/example/while.cpp | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/boost/libs/hana/example/while.cpp (limited to 'src/boost/libs/hana/example/while.cpp') diff --git a/src/boost/libs/hana/example/while.cpp b/src/boost/libs/hana/example/while.cpp new file mode 100644 index 00000000..73dc27f9 --- /dev/null +++ b/src/boost/libs/hana/example/while.cpp @@ -0,0 +1,45 @@ +// Copyright Louis Dionne 2013-2017 +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) + +#include +#include +#include +#include +#include +#include + +#include +namespace hana = boost::hana; +using namespace hana::literals; + + +int main() { + // while_ with a Constant condition (loop is unrolled at compile-time) + { + std::vector ints; + auto final_state = hana::while_(hana::less.than(10_c), 0_c, [&](auto i) { + ints.push_back(i); + return i + 1_c; + }); + + // The state is known at compile-time + BOOST_HANA_CONSTANT_CHECK(final_state == 10_c); + + BOOST_HANA_RUNTIME_CHECK(ints == std::vector{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); + } + + // while_ with a constexpr or runtime condition (loop is not unrolled) + { + std::vector ints; + int final_state = hana::while_(hana::less.than(10), 0, [&](int i) { + ints.push_back(i); + return i + 1; + }); + + // The state is known only at runtime, or at compile-time if constexpr + BOOST_HANA_RUNTIME_CHECK(final_state == 10); + + BOOST_HANA_RUNTIME_CHECK(ints == std::vector{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); + } +} -- cgit v1.2.3