1 // { dg-do run }
2 // { dg-options "-std=gnu++0x" }
3 // An implementation of TR1's <tuple> using variadic teplates
4 // Contributed by Douglas Gregor <doug.gregor@gmail.com>
5
6 #include <string>
7 #include <cassert>
8 #include <cstring>
9
10 // Trivial reference_wrapper
11 template<typename T>
12 struct reference_wrapper
13 {
reference_wrapperreference_wrapper14 reference_wrapper(T& x) : ptr(&x) { }
15
16 operator T&() const { return *ptr; }
17
18 T* ptr;
19 };
20
ref(T & x)21 template<typename T> reference_wrapper<T> ref(T& x) { return x; }
cref(const T & x)22 template<typename T> reference_wrapper<const T> cref(const T& x) { return x; }
23
24 // Simple type-traits we'll need
25 template<typename T>
26 struct add_reference
27 {
28 typedef T& type;
29 };
30
31 template<typename T>
32 struct add_reference<T&>
33 {
34 typedef T& type;
35 };
36
37 template<typename T, typename U>
38 struct is_same
39 {
40 static const bool value = false;
41 };
42
43 template<typename T>
44 struct is_same<T, T>
45 {
46 static const bool value = true;
47 };
48
49 // For creating the constructor parameters of tuple<>
50 template<typename T>
51 struct add_const_reference
52 {
53 typedef const T& type;
54 };
55
56 template<typename T>
57 struct add_const_reference<T&>
58 {
59 typedef T& type;
60 };
61
62 // 6.1.3 Class template tuple
63 template<typename... Values>
64 class tuple;
65
66 template<> class tuple<> { };
67
68 template<typename Head, typename... Tail>
69 class tuple<Head, Tail...>
70 : private tuple<Tail...>
71 {
72 typedef tuple<Tail...> inherited;
73
74 public:
75 tuple() { }
76
77 // implicit copy-constructor is okay
78
79 tuple(typename add_const_reference<Head>::type v,
80 typename add_const_reference<Tail>::type... vtail)
81 : m_head(v), inherited(vtail...) { }
82
83 template<typename... VValues>
84 tuple(const tuple<VValues...>& other)
85 : m_head(other.head()), inherited(other.tail()) { }
86
87 template<typename... VValues>
88 tuple& operator=(const tuple<VValues...>& other)
89 {
90 m_head = other.head();
91 tail() = other.tail();
92 return *this;
93 }
94
95 typename add_reference<Head>::type head() { return m_head; }
96 typename add_reference<const Head>::type head() const { return m_head; }
97 inherited& tail() { return *this; }
98 const inherited& tail() const { return *this; }
99
100 protected:
101 Head m_head;
102 };
103
104 template<typename T>
105 struct make_tuple_result
106 {
107 typedef T type;
108 };
109
110 template<typename T>
111 struct make_tuple_result<reference_wrapper<T> >
112 {
113 typedef T& type;
114 };
115
116 // 6.1.3.2 Tuple creation functions
117 struct ignore_t {
118 template<typename T> ignore_t& operator=(const T&) { return *this; }
119 } ignore;
120
121 template<typename... Values>
122 tuple<typename make_tuple_result<Values>::type...>
123 make_tuple(const Values&... values)
124 {
125 return tuple<typename make_tuple_result<Values>::type...>(values...);
126 }
127
128 template<typename... Values>
129 tuple<Values&...> tie(Values&... values)
130 {
131 return tuple<Values&...>(values...);
132 }
133
134 // 6.1.3.3 Tuple helper classes
135 template<typename Tuple>
136 struct tuple_size;
137
138 template<>
139 struct tuple_size<tuple<> >
140 {
141 static const std::size_t value = 0;
142 };
143
144 template<typename Head, typename... Tail>
145 struct tuple_size<tuple<Head, Tail...> >
146 {
147 static const std::size_t value = 1 + tuple_size<tuple<Tail...> >::value;
148 };
149
150 template<int I, typename Tuple>
151 struct tuple_element;
152
153 template<int I, typename Head, typename... Tail>
154 struct tuple_element<I, tuple<Head, Tail...> >
155 {
156 typedef typename tuple_element<I-1, tuple<Tail...> >::type type;
157 };
158
159 template<typename Head, typename... Tail>
160 struct tuple_element<0, tuple<Head, Tail...> >
161 {
162 typedef Head type;
163 };
164
165 // 6.1.3.4 Element access
166 template<int I, typename Tuple>
167 class get_impl;
168
169 template<int I, typename Head, typename... Values>
170 class get_impl<I, tuple<Head, Values...> >
171 {
172 typedef typename tuple_element<I-1, tuple<Values...> >::type Element;
173 typedef typename add_reference<Element>::type RJ;
174 typedef typename add_const_reference<Element>::type PJ;
175 typedef get_impl<I-1, tuple<Values...> > Next;
176
177 public:
178 static RJ get(tuple<Head, Values...>& t)
179 { return Next::get(t.tail()); }
180
181 static PJ get(const tuple<Head, Values...>& t)
182 { return Next::get(t.tail()); }
183 };
184
185 template<typename Head, typename... Values>
186 class get_impl<0, tuple<Head, Values...> >
187 {
188 typedef typename add_reference<Head>::type RJ;
189 typedef typename add_const_reference<Head>::type PJ;
190
191 public:
192 static RJ get(tuple<Head, Values...>& t) { return t.head(); }
193 static PJ get(const tuple<Head, Values...>& t) { return t.head(); }
194 };
195
196 template<int I, typename... Values>
197 typename add_reference<
198 typename tuple_element<I, tuple<Values...> >::type
199 >::type
200 get(tuple<Values...>& t)
201 {
202 return get_impl<I, tuple<Values...> >::get(t);
203 }
204
205 template<int I, typename... Values>
206 typename add_const_reference<
207 typename tuple_element<I, tuple<Values...> >::type
208 >::type
209 get(const tuple<Values...>& t)
210 {
211 return get_impl<I, tuple<Values...> >::get(t);
212 }
213
214 // 6.1.3.5 Relational operators
215 inline bool operator==(const tuple<>&, const tuple<>&) { return true; }
216
217 template<typename T, typename... TTail, typename U, typename... UTail>
218 bool operator==(const tuple<T, TTail...>& t, const tuple<U, UTail...>& u)
219 {
220 return t.head() == u.head() && t.tail() == u.tail();
221 }
222
223 template<typename... TValues, typename... UValues>
224 bool operator!=(const tuple<TValues...>& t, const tuple<UValues...>& u)
225 {
226 return !(t == u);
227 }
228
229 inline bool operator<(const tuple<>&, const tuple<>&) { return false; }
230
231 template<typename T, typename... TTail, typename U, typename... UTail>
232 bool operator<(const tuple<T, TTail...>& t, const tuple<U, UTail...>& u)
233 {
234 return (t.head() < u.head() ||
235 (!(t.head() < u.head()) && t.tail() < u.tail()));
236 }
237
238 template<typename... TValues, typename... UValues>
239 bool operator>(const tuple<TValues...>& t, const tuple<UValues...>& u)
240 {
241 return u < t;
242 }
243
244 template<typename... TValues, typename... UValues>
245 bool operator<=(const tuple<TValues...>& t, const tuple<UValues...>& u)
246 {
247 return !(u < t);
248 }
249
250 template<typename... TValues, typename... UValues>
251 bool operator>=(const tuple<TValues...>& t, const tuple<UValues...>& u)
252 {
253 return !(t < u);
254 }
255
256 int a0[tuple_size<tuple<> >::value == 0? 1 : -1];
257 int a1[tuple_size<tuple<int, float, double> >::value == 3? 1 : -1];
258 int a2a[is_same<tuple_element<0, tuple<int, float, double> >::type, int>
259 ::value? 1 : -1];
260 int a2b[is_same<tuple_element<1, tuple<int, float, double> >::type, float>
261 ::value? 1 : -1];
262 int a2c[is_same<tuple_element<2, tuple<int, float, double> >::type, double>
263 ::value? 1 : -1];
264
265 int main()
266 {
267 tuple<> t0;
268 tuple<int> t1(1);
269 tuple<int, float> t2(1, 3.14159f);
270 tuple<int, float, const char*> t3a(1, 3.14159f, "Hello, world!");
271 tuple<long, double, std::string> t3b(t3a);
272 t3b = t3a;
273 // t3a = t3b; DPG: triggers an error, as it should.
274
275 tuple<int, float, std::string> t3c =
276 make_tuple(17, 2.718281828, std::string("Fun"));
277
278 int seventeen = 17;
279 double pi = 3.14159;
280 tuple<int&, double&> seventeen_pi = make_tuple(ref(seventeen), ref(pi));
281 tuple<int&, const double&> seventeen_pi2 =
282 make_tuple(ref(seventeen), cref(pi));
283 tuple<int&, double&> seventeen_pi_tied = tie(seventeen, pi);
284 assert(get<0>(t3a) == 1);
285 assert(get<1>(t3a) == 3.14159f);
286 assert(std::strcmp(get<2>(t3a), "Hello, world!") == 0);
287
288 assert(t3a == t3b);
289 assert(!(t3a != t3b));
290 assert(!(t3a < t3b));
291 assert(!(t3a > t3b));
292 assert(t3a <= t3b && t3b <= t3a);
293 assert(t3a >= t3b && t3b >= t3a);
294 }
295