| 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/config.hpp> |
| 7 | #include <boost/hana/equal.hpp> |
| 8 | #include <boost/hana/for_each.hpp> |
| 9 | #include <boost/hana/integral_constant.hpp> |
| 10 | #include <boost/hana/mult.hpp> |
| 11 | #include <boost/hana/negate.hpp> |
| 12 | #include <boost/hana/plus.hpp> |
| 13 | #include <boost/hana/tuple.hpp> |
| 14 | |
| 15 | #include <string> |
| 16 | #include <vector> |
| 17 | namespace hana = boost::hana; |
| 18 | |
| 19 | |
| 20 | int main() { |
| 21 | |
| 22 | { |
| 23 | |
| 24 | //! [operators] |
| 25 | BOOST_HANA_CONSTANT_CHECK(hana::int_c<1> + hana::int_c<3> == hana::int_c<4>); |
| 26 | |
| 27 | // Mixed-type operations are supported, but only when it involves a |
| 28 | // promotion, and not a conversion that could be lossy. |
| 29 | BOOST_HANA_CONSTANT_CHECK(hana::size_c<3> * hana::ushort_c<5> == hana::size_c<15>); |
| 30 | BOOST_HANA_CONSTANT_CHECK(hana::llong_c<15> == hana::int_c<15>); |
| 31 | //! [operators] |
| 32 | |
| 33 | }{ |
| 34 | |
| 35 | //! [times_loop_unrolling] |
| 36 | std::string s; |
| 37 | for (char c = 'x'; c <= 'z'; ++c) |
| 38 | hana::int_<5>::times([&] { s += c; }); |
| 39 | |
| 40 | BOOST_HANA_RUNTIME_CHECK(s == "xxxxxyyyyyzzzzz" ); |
| 41 | //! [times_loop_unrolling] |
| 42 | |
| 43 | }{ |
| 44 | |
| 45 | //! [times_higher_order] |
| 46 | std::string s; |
| 47 | auto functions = hana::make_tuple( |
| 48 | [&] { s += "x" ; }, |
| 49 | [&] { s += "y" ; }, |
| 50 | [&] { s += "z" ; } |
| 51 | ); |
| 52 | hana::for_each(functions, hana::int_<5>::times); |
| 53 | BOOST_HANA_RUNTIME_CHECK(s == "xxxxxyyyyyzzzzz" ); |
| 54 | //! [times_higher_order] |
| 55 | |
| 56 | }{ |
| 57 | |
| 58 | //! [from_object] |
| 59 | std::string s; |
| 60 | for (char c = 'x'; c <= 'z'; ++c) |
| 61 | hana::int_c<5>.times([&] { s += c; }); |
| 62 | |
| 63 | BOOST_HANA_RUNTIME_CHECK(s == "xxxxxyyyyyzzzzz" ); |
| 64 | //! [from_object] |
| 65 | |
| 66 | }{ |
| 67 | |
| 68 | //! [times_with_index_runtime] |
| 69 | std::vector<int> v; |
| 70 | hana::int_<5>::times.with_index([&](auto index) { v.push_back(index); }); |
| 71 | |
| 72 | BOOST_HANA_RUNTIME_CHECK(v == std::vector<int>{0, 1, 2, 3, 4}); |
| 73 | //! [times_with_index_runtime] |
| 74 | |
| 75 | //! [times_with_index_compile_time] |
| 76 | constexpr auto xs = hana::tuple_c<int, 0, 1, 2>; |
| 77 | hana::int_<3>::times.with_index([xs](auto index) { |
| 78 | BOOST_HANA_CONSTANT_CHECK(xs[index] == index); |
| 79 | }); |
| 80 | //! [times_with_index_compile_time] |
| 81 | |
| 82 | }{ |
| 83 | |
| 84 | //! [literals] |
| 85 | using namespace hana::literals; // contains the _c suffix |
| 86 | |
| 87 | BOOST_HANA_CONSTANT_CHECK(1234_c == hana::llong_c<1234>); |
| 88 | BOOST_HANA_CONSTANT_CHECK(-1234_c == hana::llong_c<-1234>); |
| 89 | BOOST_HANA_CONSTANT_CHECK(1_c + (3_c * 4_c) == hana::llong_c<1 + (3 * 4)>); |
| 90 | //! [literals] |
| 91 | |
| 92 | }{ |
| 93 | |
| 94 | //! [integral_c] |
| 95 | BOOST_HANA_CONSTANT_CHECK(hana::integral_c<int, 2> == hana::int_c<2>); |
| 96 | static_assert(decltype(hana::integral_c<int, 2>)::value == 2, "" ); |
| 97 | //! [integral_c] |
| 98 | |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | |