1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2008-2019 Free Software Foundation, Inc.
5 Contributed by Red Hat, originally written by Keith Seitz.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 Please email any bugs, comments, and/or additions to this file to:
30 // A simple template with specializations
35 void do_something () { } // tclass<T>::do_something
39 void tclass
<char>::do_something () { } // tclass<char>::do_something
42 void tclass
<int>::do_something () { } // tclass<int>::do_something
45 void tclass
<long>::do_something () { } // tclass<long>::do_something
48 void tclass
<short>::do_something () { } // tclass<short>::do_something
50 // A simple template with multiple template parameters
51 template <class A
, class B
, class C
, class D
, class E
>
52 void flubber (void) { // flubber
66 // Some contrived policies
70 static void function (void) { } // operation_1<T>::function
76 static void function (void) { } // operation_2<T>::function
82 static void function (void) { } // operation_3<T>::function
88 static void function (void) { } // operation_4<T>::function
91 // A policy-based class w/ and w/o default policy
92 template <class T
, class Policy
>
93 class policy
: public Policy
96 policy (T obj
) : obj_ (obj
) { } // policy<T, Policy>::policy
102 template <class T
, class Policy
= operation_1
<T
> >
103 class policyd
: public Policy
106 policyd (T obj
) : obj_ (obj
) { } // policyd<T, Policy>::policyd
107 ~policyd (void) { } // policyd<T, Policy>::~policyd
113 typedef policy
<int, operation_1
<void*> > policy1
;
114 typedef policy
<int, operation_2
<void*> > policy2
;
115 typedef policy
<int, operation_3
<void*> > policy3
;
116 typedef policy
<int, operation_4
<void*> > policy4
;
118 typedef policyd
<int> policyd1
;
119 typedef policyd
<long> policyd2
;
120 typedef policyd
<char> policyd3
;
121 typedef policyd
<base
> policyd4
;
122 typedef policyd
<tclass
<int> > policyd5
;
125 static fluff
*g_fluff
= new fluff ();
133 base (void) : foo_ (42) { } // base::base(void)
134 base (int foo
) : foo_ (foo
) { } // base::base(int)
135 ~base (void) { } // base::~base
137 // Some overloaded methods
138 int overload (void) const { return 0; } // base::overload(void) const
139 int overload (int i
) const { return 1; } // base::overload(int) const
140 int overload (short s
) const { return 2; } // base::overload(short) const
141 int overload (long l
) const { return 3; } // base::overload(long) const
142 int overload (char* a
) const { return 4; } // base::overload(char*) const
143 int overload (base
& b
) const { return 5; } // base::overload(base&) const
146 int operator+ (base
const& o
) const { // base::operator+
147 return foo_
+ o
.foo_
; }
149 base
operator++ (void) { // base::operator++
150 ++foo_
; return *this; }
152 base
operator+=(base
const& o
) { // base::operator+=
153 foo_
+= o
.foo_
; return *this; }
155 int operator- (base
const& o
) const { // base::operator-
156 return foo_
- o
.foo_
; }
158 base
operator-- (void) { // base::operator--
159 --foo_
; return *this; }
161 base
operator-= (base
const& o
) { // base::operator-=
162 foo_
-= o
.foo_
; return *this; }
164 int operator* (base
const& o
) const { // base::operator*
165 return foo_
* o
.foo_
; }
167 base
operator*= (base
const& o
) { // base::operator*=
168 foo_
*= o
.foo_
; return *this; }
170 int operator/ (base
const& o
) const { // base::operator/
171 return foo_
/ o
.foo_
; }
173 base
operator/= (base
const& o
) { // base::operator/=
174 foo_
/= o
.foo_
; return *this; }
176 int operator% (base
const& o
) const { // base::operator%
177 return foo_
% o
.foo_
; }
179 base
operator%= (base
const& o
) { // base::operator%=
180 foo_
%= o
.foo_
; return *this; }
182 bool operator< (base
const& o
) const { // base::operator<
183 return foo_
< o
.foo_
; }
185 bool operator<= (base
const& o
) const { // base::operator<=
186 return foo_
<= o
.foo_
; }
188 bool operator> (base
const& o
) const { // base::operator>
189 return foo_
> o
.foo_
; }
191 bool operator>= (base
const& o
) const { // base::operator>=
192 return foo_
>= o
.foo_
; }
194 bool operator!= (base
const& o
) const { // base::operator!=
195 return foo_
!= o
.foo_
; }
197 bool operator== (base
const& o
) const { // base::operator==
198 return foo_
== o
.foo_
; }
200 bool operator! (void) const { // base::operator!
203 bool operator&& (base
const& o
) const { // base::operator&&
204 return foo_
&& o
.foo_
; }
206 bool operator|| (base
const& o
) const { // base::operator||
207 return foo_
|| o
.foo_
; }
209 int operator<< (int value
) const { // base::operator<<
210 return foo_
<< value
; }
212 base
operator<<= (int value
) { // base::operator<<=
213 foo_
<<= value
; return *this; }
215 int operator>> (int value
) const { // base::operator>>
216 return foo_
>> value
; }
218 base
operator>>= (int value
) { // base::operator>>=
219 foo_
>>= value
; return *this; }
221 int operator~ (void) const { // base::operator~
224 int operator& (base
const& o
) const { // base::operator&
225 return foo_
& o
.foo_
; }
227 base
operator&= (base
const& o
) { // base::operator&=
228 foo_
&= o
.foo_
; return *this; }
230 int operator| (base
const& o
) const { // base::operator|
231 return foo_
| o
.foo_
; }
233 base
operator|= (base
const& o
) { // base::operator|=
234 foo_
|= o
.foo_
; return *this; }
236 int operator^ (base
const& o
) const { // base::operator^
237 return foo_
^ o
.foo_
; }
239 base
operator^= (base
const& o
) { // base::operator^=
240 foo_
^= o
.foo_
; return *this; }
242 base
operator= (base
const& o
) { // base::operator=
243 foo_
= o
.foo_
; return *this; }
245 void operator() (void) const { // base::operator()
248 int operator[] (int idx
) const { // base::operator[]
251 void* operator new (size_t size
) throw () { // base::operator new
252 return malloc (size
); }
254 void operator delete (void* ptr
) { // base::operator delete
257 void* operator new[] (size_t size
) throw () { // base::operator new[]
258 return malloc (size
); }
260 void operator delete[] (void* ptr
) { // base::operator delete[]
263 base
const* operator-> (void) const { // base::opeartor->
266 int operator->* (base
const& b
) const { // base::operator->*
267 return foo_
* b
.foo_
; }
269 operator char* () const { return const_cast<char*> ("hello"); } // base::operator char*
270 operator int () const { return 21; } // base::operator int
271 operator fluff
* () const { return new fluff (); } // base::operator fluff*
272 operator fluff
** () const { return &g_fluff
; } // base::operator fluff**
273 operator fluff
const* const* () const { return &g_fluff
; } // base::operator fluff const* const*
276 class base1
: public virtual base
279 base1 (void) : foo_ (21) { } // base1::base1(void)
280 base1 (int a
) : foo_(a
) { } // base1::base1(int)
281 void a_function (void) const { } // base1::a_function
287 class base2
: public virtual base
290 base2 () : foo_ (3) { } // base2::base2
293 void a_function (void) const { } // base2::a_function
297 class derived
: public base1
, public base2
300 derived(void) : foo_ (4) { } // derived::derived
301 void a_function (void) const { // derived::a_function
302 this->base1::a_function ();
303 this->base2::a_function ();
316 void m(t
) const volatile;
318 const int CV::i
= 42;
320 # define ATTRIBUTE_USED __attribute__((used))
322 # define ATTRIBUTE_USED
324 ATTRIBUTE_USED
void CV::m(CV::t
) {}
325 ATTRIBUTE_USED
void CV::m(CV::t
) const {}
326 ATTRIBUTE_USED
void CV::m(CV::t
) volatile {}
327 ATTRIBUTE_USED
void CV::m(CV::t
) const volatile {}
334 test_function (int argc
, char* argv
[]) // test_function
337 void (derived::*pfunc
) (void) const = &derived::a_function
;
340 base
a (1), b (3), c (8);
341 (void) a
.overload ();
342 (void) a
.overload (static_cast<int> (0));
343 (void) a
.overload (static_cast<short> (0));
344 (void) a
.overload (static_cast<long> (0));
345 (void) a
.overload (static_cast<char*> (0));
346 (void) a
.overload (a
);
384 derived
* f
= new derived ();
385 derived
* g
= new derived
[3];
391 tclass
<char> char_tclass
;
392 tclass
<int> int_tclass
;
393 tclass
<short> short_tclass
;
394 tclass
<long> long_tclass
;
395 tclass
<base
> base_tclass
;
396 char_tclass
.do_something ();
397 int_tclass
.do_something ();
398 short_tclass
.do_something ();
399 long_tclass
.do_something ();
400 base_tclass
.do_something ();
402 flubber
<int, int, int, int, int> ();
403 flubber
<int, int, int, int, short> ();
404 flubber
<int, int, int, int, long> ();
405 flubber
<int, int, int, int, char> ();
406 flubber
<int, int, int, short, int> ();
407 flubber
<int, int, int, short, short> ();
408 flubber
<int, int, int, short, long> ();
409 flubber
<int, int, int, short, char> ();
410 flubber
<int, int, int, long, int> ();
411 flubber
<int, int, int, long, short> ();
412 flubber
<int, int, int, long, long> ();
413 flubber
<int, int, int, long, char> ();
414 flubber
<int, int, int, char, int> ();
415 flubber
<int, int, int, char, short> ();
416 flubber
<int, int, int, char, long> ();
417 flubber
<int, int, int, char, char> ();
418 flubber
<int, int, short, int, int> ();
419 flubber
<int, int, short, int, short> ();
420 flubber
<int, int, short, int, long> ();
421 flubber
<int, int, short, int, char> ();
422 flubber
<int, int, short, short, int> ();
423 flubber
<short, int, short, int, short> ();
424 flubber
<long, short, long, short, long> ();
443 policyd5
pd5 (int_tclass
);
452 fluff
const* const* flcpcp
= a
;
460 main (int argc
, char* argv
[])
464 /* Call the test function repeatedly, enough times for all our tests
465 without running forever if something goes wrong. */
466 for (i
= 0; i
< 1000; i
++)
467 test_function (argc
, argv
);