]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/hana/example/tutorial/algorithms.cpp
16a8bf614556329dcdd84061c179e6e3f8579609
1 // Copyright Louis Dionne 2013-2016
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.hpp>
6 #include <boost/hana/ext/std/integral_constant.hpp>
11 #include <type_traits>
12 namespace hana
= boost::hana
;
13 using namespace hana::literals
;
14 using namespace std::literals
;
21 //! [reverse_transform]
22 auto to_str
= [](auto const& x
) {
28 auto xs
= hana::make_tuple(1, 2.2, 'a', "bcde");
30 BOOST_HANA_RUNTIME_CHECK(
31 hana::reverse(hana::transform(xs
, to_str
)) == hana::make_tuple("bcde", "a", "2.2", "1")
33 //! [reverse_transform]
35 //! [reverse_transform_copy]
37 hana::transform(xs
, to_str
) // <-- copy into reverse(...) here?
39 //! [reverse_transform_copy]
41 //! [reverse_transform_move]
43 hana::transform(xs
, to_str
) // <-- nope, move from the temporary instead!
45 //! [reverse_transform_move]
50 auto r
= hana::any_of(hana::make_tuple("hello"s
, 1.2, 3), [](auto x
) {
51 return std::is_integral
<decltype(x
)>{};
54 BOOST_HANA_CONSTANT_CHECK(r
);
60 auto xs
= hana::make_tuple("hello"s
, 1.2, 3);
61 auto pred
= [](auto x
) { return std::is_integral
<decltype(x
)>{}; };
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 :
70 BOOST_HANA_CONSTANT_CHECK(r
);
77 //! [cross_phase.setup]
78 struct Fish
{ std::string name
; };
79 struct Cat
{ std::string name
; };
80 struct Dog
{ std::string name
; };
82 auto animals
= hana::make_tuple(Fish
{"Nemo"}, Cat
{"Garfield"}, Dog
{"Snoopy"});
83 // ^^^^^^^ not a compile-time value
85 BOOST_HANA_CONSTANT_CHECK(hana::length(animals
) == hana::size_c
<3>);
86 // ^^^^^^^^^^^^^^^^^^^^^ assertion done at compile-time
87 //! [cross_phase.setup]
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]
96 //! [cross_phase.any_of_runtime]
97 bool any_garfield
= hana::any_of(animals
, [](auto animal
) {
98 return animal
.name
== "Garfield"s
;
101 BOOST_HANA_RUNTIME_CHECK(any_garfield
);
102 //! [cross_phase.any_of_runtime]
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
>{};
111 BOOST_HANA_CONSTANT_CHECK(any_cat
);
112 //! [cross_phase.any_of_compile_time]
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
>{};
121 BOOST_HANA_CONSTANT_CHECK(any_cat
);
122 //! [cross_phase.any_of_explicit]
126 //! [cross_phase.filter]
127 auto mammals
= hana::filter(animals
, [](auto animal
) {
128 return hana::type_c
<decltype(animal
)> != hana::type_c
<Fish
>;
130 //! [cross_phase.filter]
132 BOOST_HANA_RUNTIME_CHECK(
133 hana::transform(mammals
, [](auto x
) { return x
.name
; })
134 == hana::make_tuple("Garfield", "Snoopy")
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]