C++实现的Tuple类,非常好用

C++实现的Tuple类,非常好用,新老编译器都支持。在编译器支持可变模版参数的情况下,这个Tuple支持任意多成员,不支持的情况下,支持的成员是有限的,现在只支持3个成员,需要支持跟多成员,也可以轻松扩展下。有码有真相:

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
#include <iostream>  
#include <typeinfo>

using namespace std;

#ifdef __GXX_EXPERIMENTAL_CXX0X__
template <typename... Rest> struct Tuple;
template <> struct Tuple<> {};

template <typename First, typename ... Rest>
struct Tuple<First, Rest...> : public Tuple<Rest...> {
Tuple() : value() {}
Tuple(First &&first, Rest&&... rest)
: value(std::forward<First>(first))
, Tuple<Rest...>(std::forward<Rest>(rest)...)
{}
First value;
};

template <size_t N, typename TP> struct Tuple_Element;

template <typename T, typename ... Rest>
struct Tuple_Element<0, Tuple<T, Rest...>> {
typedef T type;
typedef Tuple<T, Rest...> TPType;
};

template <size_t N, typename T, typename ... Rest>
struct Tuple_Element<N, Tuple<T, Rest...>>
: public Tuple_Element<N - 1, Tuple<Rest...>>
{};

template <size_t N, typename ... Rest>
typename Tuple_Element<N, Tuple<Rest...>>::type& get(Tuple<Rest...> &tp) {
typedef typename Tuple_Element<N, Tuple<Rest...>>::TPType type;
return ((type &)tp).value;
}

#else

struct VoidType;

template <typename T1, typename T2, typename T3>
struct Tuple;

template <>
struct Tuple<VoidType, VoidType, VoidType> {
};

template <typename T1>
struct Tuple<T1, VoidType, VoidType> {
Tuple()
{}

Tuple(const T1 &v1)
: value(v1)
{}

T1 value;
};

template <typename T1, typename T2>
struct Tuple<T1, T2, VoidType>
: public Tuple<T2, VoidType, VoidType>
{
Tuple()
{}

Tuple(const T1 &v1, const T2 &v2)
: value(v1)
, Tuple<T2, VoidType, VoidType>(v2)
{}

T1 value;
};

template <typename T1 = VoidType, typename T2 = VoidType, typename T3 = VoidType>
struct Tuple
: public Tuple<T2, T3, VoidType>
{
Tuple(const T1 &v1, const T2 &v2, const T3 &v3)
: value(v1)
, Tuple<T2, T3, VoidType>(v2, v3)
{}

T1 value;
};

template <size_t N, typename T1, typename T2, typename T3>
struct Tuple_Element;

template <>
struct Tuple_Element<0, VoidType, VoidType, VoidType> {
};

template <typename T1, typename T2, typename T3>
struct Tuple_Element<0, T1, T2, T3> {
typedef T1 type;
typedef Tuple<T1, T2, T3> TPType;
};

template <size_t N, typename T1, typename T2, typename T3>
struct Tuple_Element
: Tuple_Element<N - 1, T2, T3, VoidType>
{
};

template <size_t N, typename T1, typename T2, typename T3>
typename Tuple_Element<N, T1, T2, T3>::type& get(Tuple<T1, T2, T3> &tp) {
typedef typename Tuple_Element<N, T1, T2, T3>::TPType type;
return ((type &)tp).value;
}

#endif

int main() {
Tuple<> tmp();
//cout << typeid(get<0>(tmp)).name() << endl;
Tuple<int, string, double> tp(3, "2", 1.);
cout<<get<0>(tp) << " " << typeid(get<0>(tp)).name() <<endl;
cout<<get<1>(tp) << " " << typeid(get<1>(tp)).name() <<endl;
cout<<get<2>(tp) << " " << typeid(get<2>(tp)).name() <<endl;
cout << ((Tuple<string, double> &)tp).value << endl;
Tuple<int> ft1;
Tuple<int, float> ft2;
return 0;
}
文章目录