File size: 3,834 Bytes
8ae5fc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include <metal.hpp>

#include "example.hpp"

#if !defined(METAL_WORKAROUND)

#include <array>
#include <chrono>
#include <complex>
#include <tuple>
#include <type_traits>
#include <utility>

/// [make_array]
template<class... Xs,
    class R = std::array<std::common_type_t<Xs...>, sizeof...(Xs)>
>
constexpr R make_array(Xs&&... xs) {
    return R{{std::forward<Xs>(xs)...}};
}
/// [make_array]

/// [tuples]
using namespace std::chrono;
using namespace std::literals::chrono_literals;
using namespace std::literals::complex_literals;

auto tup1 = std::make_tuple(42ns, 0x42, 42.f);
auto tup2 = std::make_tuple(42us, 042L, 42.L);
auto tup3 = std::make_tuple(42ms, 42LL, 42.i);
/// [tuples]

#if 0
/// [naive_array_of_tuples]
auto array_of_tuples =  make_array(tup1, tup2, tup3);
/// [naive_array_of_tuples]
#endif

#include <boost/hana/config.hpp>

#if defined(BOOST_HANA_CONFIG_GCC) && \
    BOOST_HANA_CONFIG_GCC < BOOST_HANA_CONFIG_VERSION(5, 4, 0)
#   define SKIP
#endif

#if defined(BOOST_HANA_CONFIG_CLANG) && \
    BOOST_HANA_CONFIG_CLANG < BOOST_HANA_CONFIG_VERSION(3, 5, 0)
#   define SKIP
#endif

#if !defined(SKIP)

#include <boost/hana/ext/std/tuple.hpp>
#include <boost/hana/transform.hpp>
#include <boost/hana/type.hpp>
#include <boost/hana/unpack.hpp>
#include <boost/hana/zip.hpp>

namespace hana {
/// [hana_common_tuple_t]
template<class... xs>
using hana_common_tuple_t = typename decltype(
    boost::hana::unpack(
        boost::hana::zip_with(
            boost::hana::template_<std::common_type_t>,
            boost::hana::zip_with(boost::hana::decltype_, std::declval<xs>())...
        ),
        boost::hana::template_<std::tuple>
    )
)::type;
/// [hana_common_tuple_t]

/// [hana_make_array_of_tuples]
template<class... Xs,
    class R = std::array<std::common_type_t<Xs...>, sizeof...(Xs)>
>
constexpr R hana_make_array(Xs&&... xs) {
    return R{{std::forward<Xs>(xs)...}};
}

template<class Head, class... Tail,
    class R = std::array<hana_common_tuple_t<std::decay_t<Head>, std::decay_t<Tail>...>, 1 + sizeof...(Tail)>
>
constexpr R hana_make_array(Head&& head, Tail&&... tail) {
    return R{{std::forward<Head>(head), std::forward<Tail>(tail)...}};
}
/// [hana_make_array_of_tuples]

#if !defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 5000
/// [hana_array_of_tuples]
auto array_of_tuples = hana_make_array(tup1, tup2, tup3);

IS_SAME(decltype(array_of_tuples), std::array<std::tuple<nanoseconds, long long, std::complex<double>>, 3>);
/// [hana_array_of_tuples]
#endif

#if 0
/// [hana_array_of_numbers]
IS_SAME(decltype(hana_make_array(42, 42L, 42LL)), std::array<long long, 3>);
/// [hana_array_of_numbers]
#endif
}
#endif

namespace
{
/// [common_tuple_t]
template<class... xs>
using common_tuple_t = metal::apply<
    std::common_type_t<metal::lambda<std::tuple>, metal::as_lambda<xs>...>,
    metal::transform<metal::lambda<std::common_type_t>, metal::as_list<xs>...>
>;
/// [common_tuple_t]

/// [make_array_of_tuples]
template<class... Xs,
    class R = std::array<std::common_type_t<Xs...>, sizeof...(Xs)>
>
constexpr R make_array(Xs&&... xs) {
    return R{{std::forward<Xs>(xs)...}};
}

template<class Head, class... Tail,
    class R = std::array<common_tuple_t<std::decay_t<Head>, std::decay_t<Tail>...>, 1 + sizeof...(Tail)>
>
constexpr R make_array(Head&& head, Tail&&... tail) {
    return R{{std::forward<Head>(head), std::forward<Tail>(tail)...}};
}
/// [make_array_of_tuples]

#if !defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 5000
/// [array_of_tuples]
auto array_of_tuples = make_array(tup1, tup2, tup3);

IS_SAME(decltype(array_of_tuples), std::array<std::tuple<nanoseconds, long long, std::complex<double>>, 3>);
/// [array_of_tuples]
#endif

/// [array_of_numbers]
IS_SAME(decltype(make_array(42, 42L, 42LL)), std::array<long long, 3>);
/// [array_of_numbers]
}
#endif