CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

testPrimaryTraits.cc
Go to the documentation of this file.
1// ======================================================================
2//
3// Test basic functionality of primary type traits
4//
5// Author: W. E. Brown, 2010-03-24, adapted from the boost library's
6// type_traits and related functionality whose internal attributions bear
7// the following various notices:
8//
9// (C) Copyright John Maddock 2000.
10// Distributed under the Boost Software License, Version 1.0.
11// See http://www.boost.org/LICENSE_1_0.txt
12//
13// ======================================================================
14
15
16#include "CLHEP/Utility/noncopyable.h"
17#include "CLHEP/Utility/type_traits.h"
18
19#include <cassert>
20
21
22using namespace CLHEP;
23
24
25// define some test types:
26
28struct UDT
29{
30 UDT() { };
31 ~UDT() { };
32 UDT(const UDT&);
33 UDT& operator=(const UDT&);
34 int i;
35
36 void f1();
37 int f2();
38 int f3(int);
39 int f4(int, float);
40};
41
42typedef void(*f1)();
43typedef int(*f2)(int);
44typedef int(*f3)(int, bool);
45typedef void (UDT::*mf1)();
46typedef int (UDT::*mf2)();
47typedef int (UDT::*mf3)(int);
48typedef int (UDT::*mf4)(int, float);
49typedef int (UDT::*mp);
50typedef int (UDT::*cmf)(int) const;
51
52// cv-qualifiers applied to reference types should have no effect
53
54// This is intentional:
55// r_type and cr_type should be the same type
56// but some compilers wrongly apply cv-qualifiers
57// to reference types (this may generate a warning
58// on some compilers):
59
60typedef int& r_type;
61#if ! defined(_MSC_VER)
62typedef const r_type cr_type;
63#endif // _MSC_VER
64
65struct POD_UDT { int x; };
66struct empty_UDT
67{
68 empty_UDT() { };
69 empty_UDT(const empty_UDT&) { };
71 empty_UDT& operator=(const empty_UDT&){ return *this; }
72 bool operator==(const empty_UDT&)const
73 { return true; }
74};
75struct empty_POD_UDT
76{
77 bool operator==(const empty_POD_UDT&)const
78 { return true; }
79};
80union union_UDT
81{
82 int x;
83 double y;
85};
86union POD_union_UDT
87{
88 int x;
89 double y;
90};
92{
94};
95union empty_POD_union_UDT { };
96
98{
103 bool operator==(const nothrow_copy_UDT&)const
104 { return true; }
105};
106
108{
112 nothrow_assign_UDT& operator=(const nothrow_assign_UDT&)throw(){ return *this; }
114 { return true; }
115};
116
118{
124 { return true; }
125};
126
127class Base { };
128
129class Derived : public Base { };
130class Derived2 : public Base { };
131class MultiBase : public Derived, public Derived2 { };
132class PrivateBase : private Base { };
133
134class NonDerived { };
135
138
141
142struct VB
143{ virtual ~VB() { }; };
144
145struct VD : VB
146{ ~VD() { }; };
147
148// struct non_pointer:
149// used to verify that is_pointer does not return
150// true for class types that implement operator void*()
151
152struct non_pointer
153{ operator void*(){return this;} };
154struct non_int_pointer
155{
156 int i;
157 operator int*(){return &i;}
158};
161struct int_convertible
162{ operator int(); };
163
164// struct non_empty:
165// used to verify that is_empty does not emit
166// spurious warnings or errors.
167
168struct non_empty : private noncopyable
169{ int i; };
170
171// abstract base classes:
172struct test_abc1
173{
175 virtual ~test_abc1();
178 virtual void foo() = 0;
179 virtual void foo2() = 0;
180};
181
182struct test_abc2
183{
184 virtual ~test_abc2();
185 virtual void foo() = 0;
186 virtual void foo2() = 0;
187};
188
189struct test_abc3 : public test_abc1
190{ virtual void foo3() = 0; };
191
192struct incomplete_type;
193
194struct polymorphic_base
195{
197 virtual void method();
198};
199
201{ };
202
204{ virtual void method(); };
205
206struct virtual_inherit1 : virtual Base { };
208struct virtual_inherit3 : private virtual Base { };
209struct virtual_inherit4 : virtual noncopyable { };
210struct virtual_inherit5 : virtual int_convertible { };
211struct virtual_inherit6 : virtual Base { virtual ~virtual_inherit6()throw(); };
212
213typedef void foo0_t();
214typedef void foo1_t(int);
215typedef void foo2_t(int&, double);
216typedef void foo3_t(int&, bool, int, int);
217typedef void foo4_t(int, bool, int*, int[], int, int, int, int, int);
218
220{
222 int i;
223};
224
226{
228 int i;
229};
230
232{
234 int i;
235};
236
238{
240 int i;
241};
242
243template <class T>
244struct wrap
245{
246 T t;
247 int j;
248protected:
250 wrap(const wrap&);
252};
253
254
256{ operator char*() const; };
257
258
259typedef const double (UDT::*mp2) ;
260
261
262int main()
263{
264 #define claim_void(Type) (is_void<Type>::value)
265 #define has_void_type(Type) assert(claim_void(Type))
266 #define has_nonvoid_type(Type) assert(!claim_void(Type))
267
268 has_void_type(void);
269 has_void_type(void const);
270 has_void_type(void volatile);
271 has_void_type(void const volatile);
272
273 has_nonvoid_type(void*);
274 has_nonvoid_type(int);
281 has_nonvoid_type(incomplete_type);
282
283 #define claim_integral(Type) (is_integral<Type>::value)
284 #define has_integral_type(Type) assert(claim_integral(Type))
285 #define has_nonintegral_type(Type) assert(!claim_integral(Type))
286
287 has_integral_type(bool);
288 has_integral_type(bool const);
289 has_integral_type(bool volatile);
290 has_integral_type(bool const volatile);
291
292 has_integral_type(signed char);
293 has_integral_type(signed char const);
294 has_integral_type(signed char volatile);
295 has_integral_type(signed char const volatile);
296 has_integral_type(unsigned char);
297 has_integral_type(char);
298 has_integral_type(unsigned char const);
299 has_integral_type(char const);
300 has_integral_type(unsigned char volatile);
301 has_integral_type(char volatile);
302 has_integral_type(unsigned char const volatile);
303 has_integral_type(char const volatile);
304
305 has_integral_type(unsigned short);
306 has_integral_type(short);
307 has_integral_type(unsigned short const);
308 has_integral_type(short const);
309 has_integral_type(unsigned short volatile);
310 has_integral_type(short volatile);
311 has_integral_type(unsigned short const volatile);
312 has_integral_type(short const volatile);
313
314 has_integral_type(unsigned int);
316 has_integral_type(unsigned int const);
317 has_integral_type(int const);
318 has_integral_type(unsigned int volatile);
319 has_integral_type(int volatile);
320 has_integral_type(unsigned int const volatile);
321 has_integral_type(int const volatile);
322
323 has_integral_type(unsigned long);
324 has_integral_type(long);
325 has_integral_type(unsigned long const);
326 has_integral_type(long const);
327 has_integral_type(unsigned long volatile);
328 has_integral_type(long volatile);
329 has_integral_type(unsigned long const volatile);
330 has_integral_type(long const volatile);
331
339 has_nonintegral_type(const int&);
340 has_nonintegral_type(int[2]);
347 has_nonintegral_type(incomplete_type);
348
349 #define claim_floating(Type) (is_floating_point<Type>::value)
350 #define has_floating_type(Type) assert(claim_floating(Type))
351 #define has_nonfloating_type(Type) assert(!claim_floating(Type))
352
353 has_floating_type(float);
354 has_floating_type(float const);
355 has_floating_type(float volatile);
356 has_floating_type(float const volatile);
357
358 has_floating_type(double);
359 has_floating_type(double const);
360 has_floating_type(double volatile);
361 has_floating_type(double const volatile);
362
363 has_floating_type(long double);
364 has_floating_type(long double const);
365 has_floating_type(long double volatile);
366 has_floating_type(long double const volatile);
367
373 has_nonfloating_type(float*);
374 has_nonfloating_type(float&);
375 has_nonfloating_type(const float&);
376 has_nonfloating_type(float[2]);
377
384 has_nonfloating_type(incomplete_type);
385
386 #define claim_array(Type) (is_array<Type>::value)
387 #define has_array_type(Type) assert(claim_array(Type))
388 #define has_nonarray_type(Type) assert(!claim_array(Type))
389
391 has_nonarray_type(int*);
392 has_nonarray_type(const int*);
393 has_nonarray_type(const volatile int*);
394 has_nonarray_type(int*const);
395 has_nonarray_type(const int*volatile);
396 has_nonarray_type(const volatile int*const);
397 has_array_type(int[2]);
398 has_array_type(const int[2]);
399 has_array_type(const volatile int[2]);
400 has_array_type(int[2][3]);
402 has_nonarray_type(int(&)[2]);
404 has_nonarray_type(void);
409 has_nonarray_type(incomplete_type);
410
411 #define claim_ptr(Type) (is_pointer<Type>::value)
412 #define has_ptr_type(Type) assert(claim_ptr(Type))
413 #define has_nonptr_type(Type) assert(!claim_ptr(Type))
414
415 has_nonptr_type(int);
416 has_nonptr_type(int&);
417 has_ptr_type(int*);
418 has_ptr_type(const int*);
419 has_ptr_type(volatile int*);
421 has_ptr_type(int*const);
422 has_ptr_type(int*volatile);
423 has_ptr_type(int*const volatile);
425 has_nonptr_type(int*&);
426 has_nonptr_type(int(&)[2]);
427 has_nonptr_type(int[2]);
428 has_nonptr_type(char[sizeof(void*)]);
429 has_nonptr_type(void);
430
439
445
447
448 #define claim_lref(Type) (is_lvalue_reference<Type>::value)
449 #define has_lref_type(Type) assert(claim_lref(Type))
450 #define has_nonlref_type(Type) assert(!claim_lref(Type))
451
452 #define claim_ref(Type) (is_reference<Type>::value)
453 #define has_ref_type(Type) assert(claim_ref(Type))
454 #define has_nonref_type(Type) assert(!claim_ref(Type))
455
456 #define lref(Type) has_lref_type(Type); has_ref_type(Type);
457 #define nonref(Type) has_nonlref_type(Type); has_nonref_type(Type);
458
459 lref(int&);
460 lref(const int&);
461 lref(volatile int &);
462 lref(const volatile int &);
463 lref(r_type);
464 #if ! defined(_MSC_VER)
465 lref(cr_type);
466 #endif // _MSC_VER
467 lref(UDT&);
468 lref(const UDT&);
469 lref(volatile UDT&);
470 lref(const volatile UDT&);
471 lref(int (&)(int));
472 lref(int (&)[2]);
473
474 nonref(int [2]);
475 nonref(const int [2]);
476 nonref(volatile int [2]);
477 nonref(const volatile int [2]);
478 nonref(bool);
479 nonref(void);
481 nonref(foo0_t);
482 nonref(incomplete_type);
483
484 #define claim_mbrobjptr(Type) (is_member_object_pointer<Type>::value)
485 #define has_mbrobjptr_type(Type) assert(claim_mbrobjptr(Type))
486 #define has_nonmbrobjptr_type(Type) assert(!claim_mbrobjptr(Type))
487
502
503 #define claim_mbrfctnptr(Type) (is_member_function_pointer<Type>::value)
504 #define has_mbrfctnptr_type(Type) assert(claim_mbrfctnptr(Type))
505 #define has_nonmbrfctnptr_type(Type) assert(!claim_mbrfctnptr(Type))
506
521 has_nonmbrfctnptr_type(const int&);
522 has_nonmbrfctnptr_type(const int[2]);
523 has_nonmbrfctnptr_type(const int[]);
525
526 #define claim_enum(Type) (is_enum<Type>::value)
527 #define has_enum_type(Type) assert(claim_enum(Type))
528 #define has_nonenum_type(Type) assert(!claim_enum(Type))
529
530 has_nonenum_type(int);
531 has_nonenum_type(long double);
534 has_nonenum_type(int&);
536 has_nonenum_type(void);
539 has_nonenum_type(int&);
540 has_nonenum_type(const int&);
541 has_nonmbrfctnptr_type(const int[2]);
542 has_nonmbrfctnptr_type(const int[]);
544
545 return 0;
546}
#define double(obj)
Definition: excDblThrow.cc:32
void f1()
int f3(int)
int f4(int, float)
UDT(const UDT &)
UDT & operator=(const UDT &)
int f2()
virtual ~VB()
bool operator==(const empty_POD_UDT &) const
empty_UDT & operator=(const empty_UDT &)
bool operator==(const empty_UDT &) const
empty_UDT(const empty_UDT &)
nothrow_assign_UDT(const nothrow_assign_UDT &)
nothrow_assign_UDT & operator=(const nothrow_assign_UDT &)
bool operator==(const nothrow_assign_UDT &) const
bool operator==(const nothrow_construct_UDT &) const
nothrow_construct_UDT & operator=(const nothrow_construct_UDT &)
nothrow_copy_UDT & operator=(const nothrow_copy_UDT &)
bool operator==(const nothrow_copy_UDT &) const
nothrow_copy_UDT(const nothrow_copy_UDT &)
virtual ~polymorphic_base()
virtual void method()
virtual void method()
virtual void foo()=0
virtual ~test_abc1()
test_abc1 & operator=(const test_abc1 &)
test_abc1(const test_abc1 &)
virtual void foo2()=0
virtual void foo()=0
virtual void foo2()=0
virtual ~test_abc2()
virtual void foo3()=0
trivial_except_assign & operator=(trivial_except_assign const &)
trivial_except_copy(trivial_except_copy const &)
virtual ~virtual_inherit6()
wrap(const wrap &)
wrap & operator=(const wrap &)
const doubleUDT::* mp2
intUDT::* mp
int & r_type
const r_type cr_type
enum_UDT
#define has_nonintegral_type(Type)
#define has_enum_type(Type)
int(UDT::* mf2)()
#define has_nonenum_type(Type)
#define has_integral_type(Type)
int(UDT::* cmf)(int) const
int(UDT::* mf4)(int, float)
#define has_array_type(Type)
#define has_nonarray_type(Type)
int(* f2)(int)
int(* f3)(int, bool)
void(* f1)()
#define has_nonvoid_type(Type)
#define has_nonptr_type(Type)
void foo1_t(int)
#define lref(Type)
@ three_
void foo0_t()
#define has_void_type(Type)
void foo3_t(int &, bool, int, int)
#define has_nonmbrobjptr_type(Type)
void foo2_t(int &, double)
void foo4_t(int, bool, int *, int[], int, int, int, int, int)
void(UDT::* mf1)()
#define has_nonmbrfctnptr_type(Type)
#define has_mbrfctnptr_type(Type)
#define has_ptr_type(Type)
#define has_floating_type(Type)
int & r_type
#define has_mbrobjptr_type(Type)
int(UDT::* mf3)(int)
#define has_nonfloating_type(Type)
int main()
#define nonref(Type)
const r_type cr_type
@ three