- Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathtuple.hxx
49 lines (40 loc) · 1.32 KB
/
tuple.hxx
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
#pragma once
#include<utility>
#include<cstdlib>
template<typename... types_t>
structtuple_t {
// For each type in the parameter pack...
@meta for(int i = 0; i < sizeof...(types_t); ++i)
// Declare a member named _i.
types_t...[i] @(i);
};
template<size_t i, typename... types_t>
types_t...[i]& get(tuple_t<types_t...>& tuple) {
return tuple.@(i); // Allow accessing the member by name.
}
// Circle's version of tuple_element is implemented with a direct access also.
template<size_t i, typenametype_t>
structcir_tuple_element;
template<size_t i, typename... types_t>
structcir_tuple_element<i, tuple_t<types_t...> > {
typedeftypes_t...[i] type;
};
template<typename... types_t>
tuple_t<types_t...> cir_tuple(types_t&&... args) {
// Alias templates to strip a wrapper.
template <classT>
structunwrap_refwrapper {
using type = T;
};
template <classT>
structunwrap_refwrapper<std::reference_wrapper<T>> {
using type = T&;
};
// Alias template to strip references and decay types.
template <classT>
usingspecial_decay_t = typename unwrap_refwrapper<
typename std::decay<T>::type>::type;
// Expand the type and function parameter packs to return a new tuple.
typedeftuple_t<special_decay_t<types_t>...> this_tuple;
return this_tuple { std::forward<types_t>(args)... };
}