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();
121  ~nothrow_construct_UDT() { };
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 {
221  trivial_except_construct();
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 }
void(* f1)()
int(* f2)(int)
#define double(obj)
Definition: excDblThrow.cc:32
void foo1_t(int)
#define nonref(Type)
bool operator==(const nothrow_construct_UDT &) const
bool operator==(const nothrow_assign_UDT &) const
#define has_nonenum_type(Type)
nothrow_copy_UDT & operator=(const nothrow_copy_UDT &)
#define has_nonvoid_type(Type)
void foo2_t(int &, double)
empty_UDT(const empty_UDT &)
bool operator==(const nothrow_copy_UDT &) const
bool operator==(const empty_POD_UDT &) const
int & r_type
int(UDT::* mf2)()
const doubleUDT::* mp2
int main()
nothrow_construct_UDT & operator=(const nothrow_construct_UDT &)
#define has_array_type(Type)
void(UDT::* mf1)()
nothrow_assign_UDT & operator=(const nothrow_assign_UDT &)
#define has_enum_type(Type)
intUDT::* mp
int & r_type
int(UDT::* mf4)(int, float)
void foo3_t(int &, bool, int, int)
const r_type cr_type
empty_UDT & operator=(const empty_UDT &)
void foo4_t(int, bool, int *, int[], int, int, int, int, int)
virtual ~VB()
#define lref(Type)
#define has_nonptr_type(Type)
int(* f3)(int, bool)
#define has_nonintegral_type(Type)
bool operator==(const empty_UDT &) const
#define has_ptr_type(Type)
int(UDT::* mf3)(int)
#define has_mbrfctnptr_type(Type)
enum_UDT
#define has_nonfloating_type(Type)
#define has_integral_type(Type)
#define has_void_type(Type)
#define has_nonmbrfctnptr_type(Type)
#define has_nonarray_type(Type)
#define has_floating_type(Type)
const r_type cr_type
int(UDT::* cmf)(int) const
#define has_mbrobjptr_type(Type)
void foo0_t()
#define has_nonmbrobjptr_type(Type)