| 1 | // Copyright Louis Dionne 2013-2022 |
| 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> |
| 12 | namespace hana = boost::hana; |
| 13 | using namespace hana::literals; |
| 14 | using namespace std::literals; |
| 15 | |
| 16 | |
| 17 | int main() { |
| 18 | |
| 19 | { |
| 20 | |
| 21 | //! [make<tuple_tag>] |
| 22 | auto xs = hana::make<hana::tuple_tag>(1, 2.2, 'a', "bcde"s ); |
| 23 | //! [make<tuple_tag>] |
| 24 | |
| 25 | }{ |
| 26 | |
| 27 | //! [make<range_tag>] |
| 28 | constexpr auto r = hana::make<hana::range_tag>(hana::int_c<3>, hana::int_c<10>); |
| 29 | static_assert(r == hana::make_range(hana::int_c<3>, hana::int_c<10>), "" ); |
| 30 | //! [make<range_tag>] |
| 31 | |
| 32 | }{ |
| 33 | |
| 34 | //! [tuple_constructor] |
| 35 | hana::tuple<int, double, char, std::string> xs{1, 2.2, 'a', "bcde"s }; |
| 36 | //! [tuple_constructor] |
| 37 | (void)xs; |
| 38 | |
| 39 | }{ |
| 40 | |
| 41 | //! [types] |
| 42 | auto xs = hana::make_tuple(1, '2', "345" ); |
| 43 | auto 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] |
| 52 | hana::tuple<int, char, char const*> xs = hana::make_tuple(1, '2', "345" ); |
| 53 | auto 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] |
| 62 | std::string hello = "Hello" ; |
| 63 | std::vector<char> world = {'W', 'o', 'r', 'l', 'd'}; |
| 64 | |
| 65 | // hello is copied, world is moved-in |
| 66 | auto 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. |
| 70 | std::string& s = xs[0_c]; |
| 71 | //! [lifetime] |
| 72 | (void)s; |
| 73 | |
| 74 | }{ |
| 75 | |
| 76 | //! [reference_wrapper] |
| 77 | std::vector<int> ints = { /* huge vector of ints */ }; |
| 78 | std::vector<std::string> strings = { /* huge vector of strings */ }; |
| 79 | |
| 80 | auto map = hana::make_map( |
| 81 | hana::make_pair(hana::type_c<int>, std::ref(t&: ints)), |
| 82 | hana::make_pair(hana::type_c<std::string>, std::ref(t&: strings)) |
| 83 | ); |
| 84 | |
| 85 | auto& v = map[hana::type_c<int>].get(); |
| 86 | BOOST_HANA_RUNTIME_CHECK(&v == &ints); |
| 87 | //! [reference_wrapper] |
| 88 | |
| 89 | } |
| 90 | |
| 91 | } |
| 92 | |
| 93 | |
| 94 | namespace overloading { |
| 95 | //! [overloading] |
| 96 | template <typename T> |
| 97 | void f(std::vector<T> xs) { |
| 98 | // ... |
| 99 | } |
| 100 | |
| 101 | template <typename R, typename = std::enable_if_t<hana::is_a<hana::range_tag, R>()>> |
| 102 | void f(R r) { |
| 103 | // ... |
| 104 | } |
| 105 | //! [overloading] |
| 106 | } |
| 107 | |