]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/hana/example/integral_constant.cpp
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)
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>
17 namespace hana
= boost::hana
;
25 BOOST_HANA_CONSTANT_CHECK(hana::int_c
<1> + hana::int_c
<3> == hana::int_c
<4>);
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>);
35 //! [times_loop_unrolling]
37 for (char c
= 'x'; c
<= 'z'; ++c
)
38 hana::int_
<5>::times([&] { s
+= c
; });
40 BOOST_HANA_RUNTIME_CHECK(s
== "xxxxxyyyyyzzzzz");
41 //! [times_loop_unrolling]
45 //! [times_higher_order]
47 auto functions
= hana::make_tuple(
52 hana::for_each(functions
, hana::int_
<5>::times
);
53 BOOST_HANA_RUNTIME_CHECK(s
== "xxxxxyyyyyzzzzz");
54 //! [times_higher_order]
60 for (char c
= 'x'; c
<= 'z'; ++c
)
61 hana::int_c
<5>.times([&] { s
+= c
; });
63 BOOST_HANA_RUNTIME_CHECK(s
== "xxxxxyyyyyzzzzz");
68 //! [times_with_index_runtime]
70 hana::int_
<5>::times
.with_index([&](auto index
) { v
.push_back(index
); });
72 BOOST_HANA_RUNTIME_CHECK(v
== std::vector
<int>{0, 1, 2, 3, 4});
73 //! [times_with_index_runtime]
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
);
80 //! [times_with_index_compile_time]
85 using namespace hana::literals
; // contains the _c suffix
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)>);
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, "");