]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/hana/example/integral_constant.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / hana / example / integral_constant.cpp
CommitLineData
b32b8144 1// Copyright Louis Dionne 2013-2017
7c673cae
FG
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>
17namespace hana = boost::hana;
18
19
20int main() {
21
22{
23
24//! [operators]
25BOOST_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.
29BOOST_HANA_CONSTANT_CHECK(hana::size_c<3> * hana::ushort_c<5> == hana::size_c<15>);
30BOOST_HANA_CONSTANT_CHECK(hana::llong_c<15> == hana::int_c<15>);
31//! [operators]
32
33}{
34
35//! [times_loop_unrolling]
36std::string s;
37for (char c = 'x'; c <= 'z'; ++c)
38 hana::int_<5>::times([&] { s += c; });
39
40BOOST_HANA_RUNTIME_CHECK(s == "xxxxxyyyyyzzzzz");
41//! [times_loop_unrolling]
42
43}{
44
45//! [times_higher_order]
46std::string s;
b32b8144 47auto functions = hana::make_tuple(
7c673cae
FG
48 [&] { s += "x"; },
49 [&] { s += "y"; },
50 [&] { s += "z"; }
51);
52hana::for_each(functions, hana::int_<5>::times);
53BOOST_HANA_RUNTIME_CHECK(s == "xxxxxyyyyyzzzzz");
54//! [times_higher_order]
55
56}{
57
58//! [from_object]
59std::string s;
60for (char c = 'x'; c <= 'z'; ++c)
61 hana::int_c<5>.times([&] { s += c; });
62
63BOOST_HANA_RUNTIME_CHECK(s == "xxxxxyyyyyzzzzz");
64//! [from_object]
65
66}{
67
68//! [times_with_index_runtime]
69std::vector<int> v;
70hana::int_<5>::times.with_index([&](auto index) { v.push_back(index); });
71
72BOOST_HANA_RUNTIME_CHECK(v == std::vector<int>{0, 1, 2, 3, 4});
73//! [times_with_index_runtime]
74
75//! [times_with_index_compile_time]
76constexpr auto xs = hana::tuple_c<int, 0, 1, 2>;
77hana::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]
85using namespace hana::literals; // contains the _c suffix
86
87BOOST_HANA_CONSTANT_CHECK(1234_c == hana::llong_c<1234>);
88BOOST_HANA_CONSTANT_CHECK(-1234_c == hana::llong_c<-1234>);
89BOOST_HANA_CONSTANT_CHECK(1_c + (3_c * 4_c) == hana::llong_c<1 + (3 * 4)>);
90//! [literals]
91
92}{
93
94//! [integral_c]
95BOOST_HANA_CONSTANT_CHECK(hana::integral_c<int, 2> == hana::int_c<2>);
96static_assert(decltype(hana::integral_c<int, 2>)::value == 2, "");
97//! [integral_c]
98
99}
100
101}