]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/hana/example/tutorial/containers.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / hana / example / tutorial / containers.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
7#include <functional>
8#include <string>
9#include <type_traits>
10#include <utility>
11#include <vector>
12namespace hana = boost::hana;
13using namespace hana::literals;
14using namespace std::literals;
15
16
17int main() {
18
19{
20
21//! [make<tuple_tag>]
22auto xs = hana::make<hana::tuple_tag>(1, 2.2, 'a', "bcde"s);
23//! [make<tuple_tag>]
24
25}{
26
27//! [make<range_tag>]
28constexpr auto r = hana::make<hana::range_tag>(hana::int_c<3>, hana::int_c<10>);
29static_assert(r == hana::make_range(hana::int_c<3>, hana::int_c<10>), "");
30//! [make<range_tag>]
31
32}{
33
34//! [tuple_constructor]
35hana::tuple<int, double, char, std::string> xs{1, 2.2, 'a', "bcde"s};
36//! [tuple_constructor]
37(void)xs;
38
39}{
40
41//! [types]
42auto xs = hana::make_tuple(1, '2', "345");
43auto ints = hana::make_range(hana::int_c<0>, hana::int_c<100>);
44// what can we say about the types of `xs` and `ints`?
45//! [types]
46(void)xs;
47(void)ints;
48
49}{
50
51//! [types_maximally_specified]
52hana::tuple<int, char, char const*> xs = hana::make_tuple(1, '2', "345");
53auto ints = hana::make_range(hana::int_c<0>, hana::int_c<100>);
54// can't specify the type of ints, however
55//! [types_maximally_specified]
56(void)xs;
57(void)ints;
58
59}{
60
61//! [lifetime]
62std::string hello = "Hello";
63std::vector<char> world = {'W', 'o', 'r', 'l', 'd'};
64
65// hello is copied, world is moved-in
66auto xs = hana::make_tuple(hello, std::move(world));
67
68// s is a reference to the copy of hello inside xs.
69// It becomes a dangling reference as soon as xs is destroyed.
70std::string& s = xs[0_c];
71//! [lifetime]
72(void)s;
73
74}{
75
76//! [reference_wrapper]
77std::vector<int> ints = { /* huge vector of ints */ };
78std::vector<std::string> strings = { /* huge vector of strings */ };
79
80auto map = hana::make_map(
81 hana::make_pair(hana::type_c<int>, std::ref(ints)),
82 hana::make_pair(hana::type_c<std::string>, std::ref(strings))
83);
84
85auto& v = map[hana::type_c<int>].get();
86BOOST_HANA_RUNTIME_CHECK(&v == &ints);
87//! [reference_wrapper]
88
89}
90
91}
92
93
94namespace overloading {
95//! [overloading]
96template <typename T>
97void f(std::vector<T> xs) {
98 // ...
99}
100
101template <typename R, typename = std::enable_if_t<hana::is_a<hana::range_tag, R>()>>
102void f(R r) {
103 // ...
104}
105//! [overloading]
106}