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 
22 using namespace CLHEP;
23 
24 
25 // define some test types:
26 
27 enum enum_UDT{ one, two, three };
28 struct 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 
42 typedef void(*f1)();
43 typedef int(*f2)(int);
44 typedef int(*f3)(int, bool);
45 typedef void (UDT::*mf1)();
46 typedef int (UDT::*mf2)();
47 typedef int (UDT::*mf3)(int);
48 typedef int (UDT::*mf4)(int, float);
49 typedef int (UDT::*mp);
50 typedef 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 
60 typedef int& r_type;
61 #if ! defined(_MSC_VER)
62 typedef const r_type cr_type;
63 #endif // _MSC_VER
64 
65 struct POD_UDT { int x; };
66 struct empty_UDT
67 {
68  empty_UDT() { };
69  empty_UDT(const empty_UDT&) { };
70  ~empty_UDT() { };
71  empty_UDT& operator=(const empty_UDT&){ return *this; }
72  bool operator==(const empty_UDT&)const
73  { return true; }
74 };
75 struct empty_POD_UDT
76 {
77  bool operator==(const empty_POD_UDT&)const
78  { return true; }
79 };
80 union union_UDT
81 {
82  int x;
83  double y;
84  ~union_UDT() { }
85 };
86 union POD_union_UDT
87 {
88  int x;
89  double y;
90 };
91 union empty_union_UDT
92 {
94 };
95 union empty_POD_union_UDT { };
96 
97 struct nothrow_copy_UDT
98 {
100  nothrow_copy_UDT(const nothrow_copy_UDT&)throw();
102  nothrow_copy_UDT& operator=(const nothrow_copy_UDT&){ return *this; }
103  bool operator==(const nothrow_copy_UDT&)const
104  { return true; }
105 };
106 
107 struct nothrow_assign_UDT
108 {
112  nothrow_assign_UDT& operator=(const nothrow_assign_UDT&)throw(){ return *this; }
113  bool operator==(const nothrow_assign_UDT&)const
114  { return true; }
115 };
116 
118 {
119  nothrow_construct_UDT()throw();
124  { return true; }
125 };
126 
127 class Base { };
128 
129 class Derived : public Base { };
130 class Derived2 : public Base { };
131 class MultiBase : public Derived, public Derived2 { };
132 class PrivateBase : private Base { };
133 
134 class NonDerived { };
135 
136 enum enum1
137 { one_,two_ };
138 
139 enum enum2
141 
142 struct VB
143 { virtual ~VB() { }; };
144 
145 struct 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 
152 struct non_pointer
153 { operator void*(){return this;} };
154 struct non_int_pointer
155 {
156  int i;
157  operator int*(){return &i;}
158 };
159 struct int_constructible
160 { int_constructible(int); };
161 struct 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 
168 struct non_empty : private noncopyable
169 { int i; };
170 
171 // abstract base classes:
172 struct test_abc1
173 {
174  test_abc1();
175  virtual ~test_abc1();
176  test_abc1(const test_abc1&);
177  test_abc1& operator=(const test_abc1&);
178  virtual void foo() = 0;
179  virtual void foo2() = 0;
180 };
181 
182 struct test_abc2
183 {
184  virtual ~test_abc2();
185  virtual void foo() = 0;
186  virtual void foo2() = 0;
187 };
188 
189 struct test_abc3 : public test_abc1
190 { virtual void foo3() = 0; };
191 
192 struct incomplete_type;
193 
194 struct polymorphic_base
195 {
196  virtual ~polymorphic_base();
197  virtual void method();
198 };
199 
201 { };
202 
204 { virtual void method(); };
205 
206 struct virtual_inherit1 : virtual Base { };
208 struct virtual_inherit3 : private virtual Base { };
209 struct virtual_inherit4 : virtual noncopyable { };
210 struct virtual_inherit5 : virtual int_convertible { };
211 struct virtual_inherit6 : virtual Base { virtual ~virtual_inherit6()throw(); };
212 
213 typedef void foo0_t();
214 typedef void foo1_t(int);
215 typedef void foo2_t(int&, double);
216 typedef void foo3_t(int&, bool, int, int);
217 typedef 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 
231 struct trivial_except_copy
232 {
234  int i;
235 };
236 
238 {
239  trivial_except_assign& operator=(trivial_except_assign const&);
240  int i;
241 };
242 
243 template <class T>
244 struct wrap
245 {
246  T t;
247  int j;
248 protected:
249  wrap();
250  wrap(const wrap&);
251  wrap& operator=(const wrap&);
252 };
253 
254 
256 { operator char*() const; };
257 
258 
259 typedef const double (UDT::*mp2) ;
260 
261 
262 int 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);
315  has_integral_type(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 
332  has_nonintegral_type(void);
333  has_nonintegral_type(float);
337  has_nonintegral_type(int*);
338  has_nonintegral_type(int&);
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 
368  has_nonfloating_type(void);
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 
390  has_nonarray_type(int);
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]);
401  has_array_type(UDT[2]);
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 
431  has_ptr_type(f1);
432  has_ptr_type(f2);
433  has_ptr_type(f3);
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);
480  nonref(test_abc1);
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 
491  has_nonmbrobjptr_type(void*);
499  has_nonmbrobjptr_type(void);
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 
510  has_nonmbrfctnptr_type(void*);
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 }
cmf
int(UDT::* cmf)(int) const
Definition: testPrimaryTraits.cc:50
four_
@ four_
Definition: testPrimaryTraits.cc:140
empty_POD_UDT
Definition: testCategories.cc:184
polymorphic_base
Definition: testCategories.cc:303
double
#define double(obj)
Definition: excDblThrow.cc:32
nothrow_assign_UDT::~nothrow_assign_UDT
~nothrow_assign_UDT()
Definition: testPrimaryTraits.cc:111
UDT::UDT
UDT()
Definition: testPrimaryTraits.cc:30
virtual_inherit2
Definition: testCategories.cc:316
empty_POD_UDT::operator==
bool operator==(const empty_POD_UDT &) const
Definition: testPrimaryTraits.cc:77
empty_union_UDT
Definition: testCategories.cc:200
trivial_except_assign
Definition: testCategories.cc:346
empty_union_UDT::~empty_union_UDT
~empty_union_UDT()
Definition: testPrimaryTraits.cc:93
test_abc1
Definition: testCategories.cc:281
f1
void(* f1)()
Definition: testPrimaryTraits.cc:42
UDT
Definition: testCategories.cc:137
has_mbrobjptr_type
#define has_mbrobjptr_type(Type)
mp
intUDT::* mp
Definition: testCategories.cc:158
foo2_t
void foo2_t(int &, double)
Definition: testPrimaryTraits.cc:215
nothrow_copy_UDT::~nothrow_copy_UDT
~nothrow_copy_UDT()
Definition: testPrimaryTraits.cc:101
enum2
enum2
Definition: testCategories.cc:248
has_nonvoid_type
#define has_nonvoid_type(Type)
NonDerived
Definition: testCategories.cc:243
foo1_t
void foo1_t(int)
Definition: testPrimaryTraits.cc:214
one
@ one
Definition: testPrimaryTraits.cc:27
empty_UDT::operator==
bool operator==(const empty_UDT &) const
Definition: testPrimaryTraits.cc:72
polymorphic_derived2
Definition: testCategories.cc:312
virtual_inherit6
Definition: testCategories.cc:320
three_
@ three_
Definition: testPrimaryTraits.cc:140
cr_type
const typedef r_type cr_type
Definition: testPrimaryTraits.cc:62
int_constructible
Definition: testCategories.cc:268
VD::~VD
~VD()
Definition: testPrimaryTraits.cc:146
nothrow_construct_UDT
Definition: testCategories.cc:226
UDT::~UDT
~UDT()
Definition: testPrimaryTraits.cc:31
empty_UDT::empty_UDT
empty_UDT(const empty_UDT &)
Definition: testPrimaryTraits.cc:69
PrivateBase
Definition: testCategories.cc:241
nonref
#define nonref(Type)
two
@ two
Definition: testPrimaryTraits.cc:27
main
int main()
Definition: testPrimaryTraits.cc:262
nothrow_assign_UDT::operator=
nothrow_assign_UDT & operator=(const nothrow_assign_UDT &)
Definition: testPrimaryTraits.cc:112
test_abc2
Definition: testCategories.cc:291
has_nonenum_type
#define has_nonenum_type(Type)
has_enum_type
#define has_enum_type(Type)
trivial_except_copy
Definition: testCategories.cc:340
empty_UDT::empty_UDT
empty_UDT()
Definition: testPrimaryTraits.cc:68
r_type
int & r_type
Definition: testPrimaryTraits.cc:60
trivial_except_destroy
Definition: testCategories.cc:334
non_int_pointer
Definition: testCategories.cc:263
mp2
const typedef doubleUDT::* mp2
Definition: testPrimaryTraits.cc:259
polymorphic_derived1
Definition: testCategories.cc:309
POD_union_UDT
Definition: testCategories.cc:195
f3
int(* f3)(int, bool)
Definition: testPrimaryTraits.cc:44
empty_UDT::~empty_UDT
~empty_UDT()
Definition: testPrimaryTraits.cc:70
mf2
int(UDT::* mf2)()
Definition: testPrimaryTraits.cc:46
mf3
int(UDT::* mf3)(int)
Definition: testPrimaryTraits.cc:47
two_
@ two_
Definition: testPrimaryTraits.cc:137
foo4_t
void foo4_t(int, bool, int *, int[], int, int, int, int, int)
Definition: testPrimaryTraits.cc:217
union_UDT
Definition: testCategories.cc:189
nothrow_construct_UDT::operator=
nothrow_construct_UDT & operator=(const nothrow_construct_UDT &)
Definition: testPrimaryTraits.cc:122
foo3_t
void foo3_t(int &, bool, int, int)
Definition: testPrimaryTraits.cc:216
CLHEP
Definition: ClhepVersion.h:13
Base
Definition: testCategories.cc:236
union_UDT::~union_UDT
~union_UDT()
Definition: testPrimaryTraits.cc:84
has_array_type
#define has_array_type(Type)
void
We should separate methods that force the load of the Rotation class For practical that implies that methods like and that as in the ThreeVector class we separate the cc files Also again we have the rotation methods returning HepLorentzVector &rather than void
Definition: minorMergeIssues.doc:148
virtual_inherit3
Definition: testCategories.cc:317
wrap
Definition: testCategories.cc:353
three
@ three
Definition: testPrimaryTraits.cc:27
empty_UDT::operator=
empty_UDT & operator=(const empty_UDT &)
Definition: testPrimaryTraits.cc:71
int_convertible
Definition: testCategories.cc:270
nothrow_copy_UDT::operator=
nothrow_copy_UDT & operator=(const nothrow_copy_UDT &)
Definition: testPrimaryTraits.cc:102
has_nonfloating_type
#define has_nonfloating_type(Type)
empty_UDT
Definition: testCategories.cc:175
has_mbrfctnptr_type
#define has_mbrfctnptr_type(Type)
MultiBase
Definition: testCategories.cc:240
nothrow_assign_UDT::operator==
bool operator==(const nothrow_assign_UDT &) const
Definition: testPrimaryTraits.cc:113
VB
Definition: testCategories.cc:251
f2
int(* f2)(int)
Definition: testPrimaryTraits.cc:43
has_nonintegral_type
#define has_nonintegral_type(Type)
trivial_except_construct
Definition: testCategories.cc:328
enum_UDT
enum_UDT
Definition: testCategories.cc:136
enum1
enum1
Definition: testCategories.cc:245
j
long j
Definition: JamesRandomSeeding.txt:28
lref
#define lref(Type)
has_nonarray_type
#define has_nonarray_type(Type)
non_empty
Definition: testCategories.cc:277
has_nonptr_type
#define has_nonptr_type(Type)
VB::~VB
virtual ~VB()
Definition: testPrimaryTraits.cc:143
i
long i
Definition: JamesRandomSeeding.txt:27
VD
Definition: testCategories.cc:254
virtual_inherit4
Definition: testCategories.cc:318
Derived
Definition: testCategories.cc:238
r_type
int & r_type
Definition: testCategories.cc:169
nothrow_construct_UDT::operator==
bool operator==(const nothrow_construct_UDT &) const
Definition: testPrimaryTraits.cc:123
has_ptr_type
#define has_ptr_type(Type)
test_abc3
Definition: testCategories.cc:298
nothrow_copy_UDT
Definition: testCategories.cc:206
mf4
int(UDT::* mf4)(int, float)
Definition: testPrimaryTraits.cc:48
foo0_t
void foo0_t()
Definition: testPrimaryTraits.cc:213
CLHEP::noncopyable
Definition: Matrix/CLHEP/Utility/noncopyable.h:18
nothrow_assign_UDT
Definition: testCategories.cc:216
virtual_inherit1
Definition: testCategories.cc:315
empty_POD_union_UDT
Definition: testCategories.cc:204
x
any side effects of that construction would occur twice The semantics of throw x
Definition: whyZMthrowRethrows.txt:37
has_nonmbrfctnptr_type
#define has_nonmbrfctnptr_type(Type)
has_nonmbrobjptr_type
#define has_nonmbrobjptr_type(Type)
mf1
void(UDT::* mf1)()
Definition: testPrimaryTraits.cc:45
convertible_to_pointer
Definition: testCategories.cc:364
POD_UDT
Definition: testCategories.cc:174
non_pointer
Definition: testCategories.cc:261
one_
@ one_
Definition: testPrimaryTraits.cc:137
Derived2
Definition: testCategories.cc:239
nothrow_copy_UDT::operator==
bool operator==(const nothrow_copy_UDT &) const
Definition: testPrimaryTraits.cc:103
has_integral_type
#define has_integral_type(Type)
virtual_inherit5
Definition: testCategories.cc:319
has_floating_type
#define has_floating_type(Type)
has_void_type
#define has_void_type(Type)