OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [testsuite/] [g++.dg/] [cpp0x/] [variadic-tuple.C] - Blame information for rev 705

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.