]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/hana/example/tutorial/algorithms.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / hana / example / tutorial / algorithms.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)
4
5 #include <boost/hana.hpp>
6 #include <boost/hana/ext/std/integral_constant.hpp>
7
8 #include <sstream>
9 #include <string>
10 #include <tuple>
11 #include <type_traits>
12 namespace hana = boost::hana;
13 using namespace hana::literals;
14 using namespace std::literals;
15
16
17 int main() {
18
19 {
20
21 //! [reverse_transform]
22 auto to_str = [](auto const& x) {
23 std::stringstream ss;
24 ss << x;
25 return ss.str();
26 };
27
28 auto xs = hana::make_tuple(1, 2.2, 'a', "bcde");
29
30 BOOST_HANA_RUNTIME_CHECK(
31 hana::reverse(hana::transform(xs, to_str)) == hana::make_tuple("bcde", "a", "2.2", "1")
32 );
33 //! [reverse_transform]
34
35 //! [reverse_transform_copy]
36 hana::reverse(
37 hana::transform(xs, to_str) // <-- copy into reverse(...) here?
38 );
39 //! [reverse_transform_copy]
40
41 //! [reverse_transform_move]
42 hana::reverse(
43 hana::transform(xs, to_str) // <-- nope, move from the temporary instead!
44 );
45 //! [reverse_transform_move]
46
47 }{
48
49 //! [effects]
50 auto r = hana::any_of(hana::make_tuple("hello"s, 1.2, 3), [](auto x) {
51 return std::is_integral<decltype(x)>{};
52 });
53
54 BOOST_HANA_CONSTANT_CHECK(r);
55 //! [effects]
56
57 {
58
59 //! [effects.codegen]
60 auto xs = hana::make_tuple("hello"s, 1.2, 3);
61 auto pred = [](auto x) { return std::is_integral<decltype(x)>{}; };
62
63 auto r = hana::bool_c<
64 decltype(pred(xs[0_c]))::value ? true :
65 decltype(pred(xs[1_c]))::value ? true :
66 decltype(pred(xs[2_c]))::value ? true :
67 false
68 >;
69
70 BOOST_HANA_CONSTANT_CHECK(r);
71 //! [effects.codegen]
72
73 }
74
75 }{
76
77 //! [cross_phase.setup]
78 struct Fish { std::string name; };
79 struct Cat { std::string name; };
80 struct Dog { std::string name; };
81
82 auto animals = hana::make_tuple(Fish{"Nemo"}, Cat{"Garfield"}, Dog{"Snoopy"});
83 // ^^^^^^^ not a compile-time value
84
85 BOOST_HANA_CONSTANT_CHECK(hana::length(animals) == hana::size_c<3>);
86 // ^^^^^^^^^^^^^^^^^^^^^ assertion done at compile-time
87 //! [cross_phase.setup]
88
89 //! [cross_phase.is_empty]
90 BOOST_HANA_CONSTANT_CHECK(!hana::is_empty(animals));
91 // ^^^^^^^^^^^^^^^^^^^^^^^ assertion done at compile-time
92 //! [cross_phase.is_empty]
93
94 {
95
96 //! [cross_phase.any_of_runtime]
97 bool any_garfield = hana::any_of(animals, [](auto animal) {
98 return animal.name == "Garfield"s;
99 });
100
101 BOOST_HANA_RUNTIME_CHECK(any_garfield);
102 //! [cross_phase.any_of_runtime]
103
104 }{
105
106 //! [cross_phase.any_of_compile_time]
107 auto any_cat = hana::any_of(animals, [](auto x) {
108 return std::is_same<decltype(x), Cat>{};
109 });
110
111 BOOST_HANA_CONSTANT_CHECK(any_cat);
112 //! [cross_phase.any_of_compile_time]
113
114 }{
115
116 //! [cross_phase.any_of_explicit]
117 hana::integral_constant<bool, true> any_cat = hana::any_of(animals, [](auto x) {
118 return std::is_same<decltype(x), Cat>{};
119 });
120
121 BOOST_HANA_CONSTANT_CHECK(any_cat);
122 //! [cross_phase.any_of_explicit]
123
124 }{
125
126 //! [cross_phase.filter]
127 auto mammals = hana::filter(animals, [](auto animal) {
128 return hana::type_c<decltype(animal)> != hana::type_c<Fish>;
129 });
130 //! [cross_phase.filter]
131
132 BOOST_HANA_RUNTIME_CHECK(
133 hana::transform(mammals, [](auto x) { return x.name; })
134 == hana::make_tuple("Garfield", "Snoopy")
135 );
136
137 }
138
139 }{
140
141 //! [cross_phase.std::tuple_size]
142 std::tuple<int, char, std::string> xs{1, '2', std::string{"345"}};
143 static_assert(std::tuple_size<decltype(xs)>::value == 3u, "");
144 //! [cross_phase.std::tuple_size]
145
146 }
147
148 }