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