-
Notifications
You must be signed in to change notification settings - Fork 3
/
tuple_util.hpp
55 lines (46 loc) · 1.39 KB
/
tuple_util.hpp
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
#ifndef _TUPLE_UTIL_HPP_
#define _TUPLE_UTIL_HPP_
/*
Insertion operator (<<), tuple_tail and tuple_reverse for a
C++11-style tuple. Needs tuple, make_tuple, and get defined.
Copyright Paul Keir 2012-2016
Distributed under the Boost Software License, Version 1.0.
(See accompanying file license.txt or copy at http://boost.org/LICENSE_1_0.txt)
*/
template <typename ...Ts, size_t ...Is>
constexpr
auto
select(tuple<Ts...> t, indices<Is...>) ->
decltype(make_tuple( get<Is>(t)... )) {
return make_tuple( get<Is>(t)... );
}
template <typename T, typename ...Ts>
constexpr
tuple<Ts...>
tuple_tail(tuple<T,Ts...> t) {
return select(t, mk_index_range<1,sizeof...(Ts)>()); // n.b T,Ts; not Ts
}
template <typename ...Ts>
constexpr
tuple<Ts...>
tuple_reverse(tuple<Ts...> t) {
return select(t, mk_index_range<sizeof...(Ts)-1,0,-1>());
}
void print_tuple(const tuple<> &t, std::ostream &o = std::cout) {}
template <typename T>
void print_tuple(const tuple<T> &t, std::ostream &o = std::cout) {
o << get<0>(t);
}
template <typename T1, typename T2, typename ...Ts>
void print_tuple(const tuple<T1,T2,Ts...> &t, std::ostream &o = std::cout) {
o << get<0>(t) << ",";
print_tuple(tuple_tail(t),o);
}
template <typename ...Ts>
inline std::ostream &operator << (std::ostream &o, const tuple<Ts...> &t) {
std::cout << "(";
print_tuple(t,o);
std::cout << ")";
return o;
}
#endif // _TUPLE_UTIL_HPP_