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