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

testPrimaryTraits.cc

Go to the documentation of this file.
00001 // ======================================================================
00002 //
00003 // Test basic functionality of primary type traits
00004 //
00005 // Author:  W. E. Brown, 2010-03-24, adapted from the boost library's
00006 // type_traits and related functionality whose internal attributions bear
00007 // the following various notices:
00008 //
00009 //   (C) Copyright John Maddock 2000.
00010 //   Distributed under the Boost Software License, Version 1.0.
00011 //   See http://www.boost.org/LICENSE_1_0.txt
00012 //
00013 // ======================================================================
00014 
00015 
00016 #include "CLHEP/Utility/noncopyable.h"
00017 #include "CLHEP/Utility/type_traits.h"
00018 
00019 #include <cassert>
00020 
00021 
00022 using namespace CLHEP;
00023 
00024 
00025 // define some test types:
00026 
00027 enum enum_UDT{ one, two, three };
00028 struct UDT
00029 {
00030   UDT() { };
00031   ~UDT() { };
00032   UDT(const UDT&);
00033   UDT& operator=(const UDT&);
00034   int i;
00035 
00036   void f1();
00037   int f2();
00038   int f3(int);
00039   int f4(int, float);
00040 };
00041 
00042 typedef void(*f1)();
00043 typedef int(*f2)(int);
00044 typedef int(*f3)(int, bool);
00045 typedef void (UDT::*mf1)();
00046 typedef int (UDT::*mf2)();
00047 typedef int (UDT::*mf3)(int);
00048 typedef int (UDT::*mf4)(int, float);
00049 typedef int (UDT::*mp);
00050 typedef int (UDT::*cmf)(int) const;
00051 
00052 // cv-qualifiers applied to reference types should have no effect
00053 
00054 // This is intentional:
00055 // r_type and cr_type should be the same type
00056 // but some compilers wrongly apply cv-qualifiers
00057 // to reference types (this may generate a warning
00058 // on some compilers):
00059 
00060 typedef int& r_type;
00061 #if ! defined(_MSC_VER)
00062 typedef const r_type cr_type;
00063 #endif  // _MSC_VER
00064 
00065 struct POD_UDT { int x; };
00066 struct empty_UDT
00067 {
00068   empty_UDT() { };
00069   empty_UDT(const empty_UDT&) { };
00070   ~empty_UDT() { };
00071   empty_UDT& operator=(const empty_UDT&){ return *this; }
00072   bool operator==(const empty_UDT&)const
00073   { return true; }
00074 };
00075 struct empty_POD_UDT
00076 {
00077   bool operator==(const empty_POD_UDT&)const
00078   { return true; }
00079 };
00080 union union_UDT
00081 {
00082   int x;
00083   double y;
00084   ~union_UDT() { }
00085 };
00086 union POD_union_UDT
00087 {
00088   int x;
00089   double y;
00090 };
00091 union empty_union_UDT
00092 {
00093   ~empty_union_UDT() { }
00094 };
00095 union empty_POD_union_UDT { };
00096 
00097 struct nothrow_copy_UDT
00098 {
00099   nothrow_copy_UDT();
00100   nothrow_copy_UDT(const nothrow_copy_UDT&)throw();
00101   ~nothrow_copy_UDT() { };
00102   nothrow_copy_UDT& operator=(const nothrow_copy_UDT&){ return *this; }
00103   bool operator==(const nothrow_copy_UDT&)const
00104   { return true; }
00105 };
00106 
00107 struct nothrow_assign_UDT
00108 {
00109   nothrow_assign_UDT();
00110   nothrow_assign_UDT(const nothrow_assign_UDT&);
00111   ~nothrow_assign_UDT() { };
00112   nothrow_assign_UDT& operator=(const nothrow_assign_UDT&)throw(){ return *this; }
00113   bool operator==(const nothrow_assign_UDT&)const
00114   { return true; }
00115 };
00116 
00117 struct nothrow_construct_UDT
00118 {
00119   nothrow_construct_UDT()throw();
00120   nothrow_construct_UDT(const nothrow_construct_UDT&);
00121   ~nothrow_construct_UDT() { };
00122   nothrow_construct_UDT& operator=(const nothrow_construct_UDT&){ return *this; }
00123   bool operator==(const nothrow_construct_UDT&)const
00124   { return true; }
00125 };
00126 
00127 class Base { };
00128 
00129 class Derived : public Base { };
00130 class Derived2 : public Base { };
00131 class MultiBase : public Derived, public Derived2  { };
00132 class PrivateBase : private Base { };
00133 
00134 class NonDerived { };
00135 
00136 enum enum1
00137 { one_,two_ };
00138 
00139 enum enum2
00140 { three_,four_ };
00141 
00142 struct VB
00143 { virtual ~VB() { }; };
00144 
00145 struct VD : VB
00146 { ~VD() { }; };
00147 
00148 // struct non_pointer:
00149 // used to verify that is_pointer does not return
00150 // true for class types that implement operator void*()
00151 
00152 struct non_pointer
00153 { operator void*(){return this;} };
00154 struct non_int_pointer
00155 {
00156   int i;
00157   operator int*(){return &i;}
00158 };
00159 struct int_constructible
00160 { int_constructible(int); };
00161 struct int_convertible
00162 { operator int(); };
00163 
00164 // struct non_empty:
00165 // used to verify that is_empty does not emit
00166 // spurious warnings or errors.
00167 
00168 struct non_empty : private noncopyable
00169 { int i; };
00170 
00171 // abstract base classes:
00172 struct test_abc1
00173 {
00174   test_abc1();
00175   virtual ~test_abc1();
00176   test_abc1(const test_abc1&);
00177   test_abc1& operator=(const test_abc1&);
00178   virtual void foo() = 0;
00179   virtual void foo2() = 0;
00180 };
00181 
00182 struct test_abc2
00183 {
00184   virtual ~test_abc2();
00185   virtual void foo() = 0;
00186   virtual void foo2() = 0;
00187 };
00188 
00189 struct test_abc3 : public test_abc1
00190 { virtual void foo3() = 0; };
00191 
00192 struct incomplete_type;
00193 
00194 struct polymorphic_base
00195 {
00196   virtual ~polymorphic_base();
00197   virtual void method();
00198 };
00199 
00200 struct polymorphic_derived1 : polymorphic_base
00201 { };
00202 
00203 struct polymorphic_derived2 : polymorphic_base
00204 { virtual void method(); };
00205 
00206 struct virtual_inherit1 : virtual Base { };
00207 struct virtual_inherit2 : virtual_inherit1 { };
00208 struct virtual_inherit3 : private virtual Base { };
00209 struct virtual_inherit4 : virtual noncopyable { };
00210 struct virtual_inherit5 : virtual int_convertible { };
00211 struct virtual_inherit6 : virtual Base { virtual ~virtual_inherit6()throw(); };
00212 
00213 typedef void foo0_t();
00214 typedef void foo1_t(int);
00215 typedef void foo2_t(int&, double);
00216 typedef void foo3_t(int&, bool, int, int);
00217 typedef void foo4_t(int, bool, int*, int[], int, int, int, int, int);
00218 
00219 struct trivial_except_construct
00220 {
00221   trivial_except_construct();
00222   int i;
00223 };
00224 
00225 struct trivial_except_destroy
00226 {
00227   ~trivial_except_destroy();
00228   int i;
00229 };
00230 
00231 struct trivial_except_copy
00232 {
00233   trivial_except_copy(trivial_except_copy const&);
00234   int i;
00235 };
00236 
00237 struct trivial_except_assign
00238 {
00239   trivial_except_assign& operator=(trivial_except_assign const&);
00240   int i;
00241 };
00242 
00243 template <class T>
00244 struct wrap
00245 {
00246   T t;
00247   int j;
00248 protected:
00249   wrap();
00250   wrap(const wrap&);
00251   wrap& operator=(const wrap&);
00252 };
00253 
00254 
00255 struct convertible_to_pointer
00256 { operator char*() const; };
00257 
00258 
00259 typedef const double (UDT::*mp2) ;
00260 
00261 
00262 int main()
00263 {
00264   #define claim_void(Type)       (is_void<Type>::value)
00265   #define has_void_type(Type)    assert(claim_void(Type))
00266   #define has_nonvoid_type(Type) assert(!claim_void(Type))
00267 
00268   has_void_type(void);
00269   has_void_type(void const);
00270   has_void_type(void volatile);
00271   has_void_type(void const volatile);
00272 
00273   has_nonvoid_type(void*);
00274   has_nonvoid_type(int);
00275   has_nonvoid_type(test_abc1);
00276   has_nonvoid_type(foo0_t);
00277   has_nonvoid_type(foo1_t);
00278   has_nonvoid_type(foo2_t);
00279   has_nonvoid_type(foo3_t);
00280   has_nonvoid_type(foo4_t);
00281   has_nonvoid_type(incomplete_type);
00282 
00283   #define claim_integral(Type)       (is_integral<Type>::value)
00284   #define has_integral_type(Type)    assert(claim_integral(Type))
00285   #define has_nonintegral_type(Type) assert(!claim_integral(Type))
00286 
00287   has_integral_type(bool);
00288   has_integral_type(bool const);
00289   has_integral_type(bool volatile);
00290   has_integral_type(bool const volatile);
00291 
00292   has_integral_type(signed char);
00293   has_integral_type(signed char const);
00294   has_integral_type(signed char volatile);
00295   has_integral_type(signed char const volatile);
00296   has_integral_type(unsigned char);
00297   has_integral_type(char);
00298   has_integral_type(unsigned char const);
00299   has_integral_type(char const);
00300   has_integral_type(unsigned char volatile);
00301   has_integral_type(char volatile);
00302   has_integral_type(unsigned char const volatile);
00303   has_integral_type(char const volatile);
00304 
00305   has_integral_type(unsigned short);
00306   has_integral_type(short);
00307   has_integral_type(unsigned short const);
00308   has_integral_type(short const);
00309   has_integral_type(unsigned short volatile);
00310   has_integral_type(short volatile);
00311   has_integral_type(unsigned short const volatile);
00312   has_integral_type(short const volatile);
00313 
00314   has_integral_type(unsigned int);
00315   has_integral_type(int);
00316   has_integral_type(unsigned int const);
00317   has_integral_type(int const);
00318   has_integral_type(unsigned int volatile);
00319   has_integral_type(int volatile);
00320   has_integral_type(unsigned int const volatile);
00321   has_integral_type(int const volatile);
00322 
00323   has_integral_type(unsigned long);
00324   has_integral_type(long);
00325   has_integral_type(unsigned long const);
00326   has_integral_type(long const);
00327   has_integral_type(unsigned long volatile);
00328   has_integral_type(long volatile);
00329   has_integral_type(unsigned long const volatile);
00330   has_integral_type(long const volatile);
00331 
00332   has_nonintegral_type(void);
00333   has_nonintegral_type(float);
00334   has_nonintegral_type(UDT);
00335   has_nonintegral_type(test_abc1);
00336   has_nonintegral_type(empty_UDT);
00337   has_nonintegral_type(int*);
00338   has_nonintegral_type(int&);
00339   has_nonintegral_type(const int&);
00340   has_nonintegral_type(int[2]);
00341   has_nonintegral_type(test_abc1);
00342   has_nonintegral_type(foo0_t);
00343   has_nonintegral_type(foo1_t);
00344   has_nonintegral_type(foo2_t);
00345   has_nonintegral_type(foo3_t);
00346   has_nonintegral_type(foo4_t);
00347   has_nonintegral_type(incomplete_type);
00348 
00349   #define claim_floating(Type)       (is_floating_point<Type>::value)
00350   #define has_floating_type(Type)    assert(claim_floating(Type))
00351   #define has_nonfloating_type(Type) assert(!claim_floating(Type))
00352 
00353   has_floating_type(float);
00354   has_floating_type(float const);
00355   has_floating_type(float volatile);
00356   has_floating_type(float const volatile);
00357 
00358   has_floating_type(double);
00359   has_floating_type(double const);
00360   has_floating_type(double volatile);
00361   has_floating_type(double const volatile);
00362 
00363   has_floating_type(long double);
00364   has_floating_type(long double const);
00365   has_floating_type(long double volatile);
00366   has_floating_type(long double const volatile);
00367 
00368   has_nonfloating_type(void);
00369   has_nonfloating_type(int);
00370   has_nonfloating_type(UDT);
00371   has_nonfloating_type(test_abc1);
00372   has_nonfloating_type(empty_UDT);
00373   has_nonfloating_type(float*);
00374   has_nonfloating_type(float&);
00375   has_nonfloating_type(const float&);
00376   has_nonfloating_type(float[2]);
00377 
00378   has_nonfloating_type(test_abc1);
00379   has_nonfloating_type(foo0_t);
00380   has_nonfloating_type(foo1_t);
00381   has_nonfloating_type(foo2_t);
00382   has_nonfloating_type(foo3_t);
00383   has_nonfloating_type(foo4_t);
00384   has_nonfloating_type(incomplete_type);
00385 
00386   #define claim_array(Type)       (is_array<Type>::value)
00387   #define has_array_type(Type)    assert(claim_array(Type))
00388   #define has_nonarray_type(Type) assert(!claim_array(Type))
00389 
00390   has_nonarray_type(int);
00391   has_nonarray_type(int*);
00392   has_nonarray_type(const int*);
00393   has_nonarray_type(const volatile int*);
00394   has_nonarray_type(int*const);
00395   has_nonarray_type(const int*volatile);
00396   has_nonarray_type(const volatile int*const);
00397   has_array_type(int[2]);
00398   has_array_type(const int[2]);
00399   has_array_type(const volatile int[2]);
00400   has_array_type(int[2][3]);
00401   has_array_type(UDT[2]);
00402   has_nonarray_type(int(&)[2]);
00403   has_nonarray_type(f1);
00404   has_nonarray_type(void);
00405   has_nonarray_type(test_abc1);
00406   has_nonarray_type(convertible_to_pointer);
00407   has_nonarray_type(test_abc1);
00408   has_nonarray_type(foo0_t);
00409   has_nonarray_type(incomplete_type);
00410 
00411   #define claim_ptr(Type)       (is_pointer<Type>::value)
00412   #define has_ptr_type(Type)    assert(claim_ptr(Type))
00413   #define has_nonptr_type(Type) assert(!claim_ptr(Type))
00414 
00415   has_nonptr_type(int);
00416   has_nonptr_type(int&);
00417   has_ptr_type(int*);
00418   has_ptr_type(const int*);
00419   has_ptr_type(volatile int*);
00420   has_ptr_type(non_pointer*);
00421   has_ptr_type(int*const);
00422   has_ptr_type(int*volatile);
00423   has_ptr_type(int*const volatile);
00424   has_nonptr_type(non_pointer);
00425   has_nonptr_type(int*&);
00426   has_nonptr_type(int(&)[2]);
00427   has_nonptr_type(int[2]);
00428   has_nonptr_type(char[sizeof(void*)]);
00429   has_nonptr_type(void);
00430 
00431   has_ptr_type(f1);
00432   has_ptr_type(f2);
00433   has_ptr_type(f3);
00434   has_nonptr_type(mf1);
00435   has_nonptr_type(mf2);
00436   has_nonptr_type(mf3);
00437   has_nonptr_type(mf4);
00438   has_nonptr_type(test_abc1);
00439 
00440   has_nonptr_type(foo0_t);
00441   has_nonptr_type(foo1_t);
00442   has_nonptr_type(foo2_t);
00443   has_nonptr_type(foo3_t);
00444   has_nonptr_type(foo4_t);
00445 
00446   has_nonptr_type(test_abc1);
00447 
00448   #define claim_lref(Type)       (is_lvalue_reference<Type>::value)
00449   #define has_lref_type(Type)    assert(claim_lref(Type))
00450   #define has_nonlref_type(Type) assert(!claim_lref(Type))
00451 
00452   #define claim_ref(Type)       (is_reference<Type>::value)
00453   #define has_ref_type(Type)    assert(claim_ref(Type))
00454   #define has_nonref_type(Type) assert(!claim_ref(Type))
00455 
00456   #define lref(Type)   has_lref_type(Type);    has_ref_type(Type);
00457   #define nonref(Type) has_nonlref_type(Type); has_nonref_type(Type);
00458 
00459   lref(int&);
00460   lref(const int&);
00461   lref(volatile int &);
00462   lref(const volatile int &);
00463   lref(r_type);
00464   #if ! defined(_MSC_VER)
00465   lref(cr_type);
00466   #endif  // _MSC_VER
00467   lref(UDT&);
00468   lref(const UDT&);
00469   lref(volatile UDT&);
00470   lref(const volatile UDT&);
00471   lref(int (&)(int));
00472   lref(int (&)[2]);
00473 
00474   nonref(int [2]);
00475   nonref(const int [2]);
00476   nonref(volatile int [2]);
00477   nonref(const volatile int [2]);
00478   nonref(bool);
00479   nonref(void);
00480   nonref(test_abc1);
00481   nonref(foo0_t);
00482   nonref(incomplete_type);
00483 
00484   #define claim_mbrobjptr(Type)       (is_member_object_pointer<Type>::value)
00485   #define has_mbrobjptr_type(Type)    assert(claim_mbrobjptr(Type))
00486   #define has_nonmbrobjptr_type(Type) assert(!claim_mbrobjptr(Type))
00487 
00488   has_nonmbrobjptr_type(f1);
00489   has_nonmbrobjptr_type(f2);
00490   has_nonmbrobjptr_type(f3);
00491   has_nonmbrobjptr_type(void*);
00492   has_nonmbrobjptr_type(mf1);
00493   has_nonmbrobjptr_type(mf2);
00494   has_nonmbrobjptr_type(mf3);
00495   has_nonmbrobjptr_type(mf4);
00496   has_nonmbrobjptr_type(cmf);
00497   has_mbrobjptr_type(mp);
00498   has_mbrobjptr_type(mp2);
00499   has_nonmbrobjptr_type(void);
00500   has_nonmbrobjptr_type(test_abc1);
00501   has_nonmbrobjptr_type(foo0_t);
00502 
00503   #define claim_mbrfctnptr(Type)       (is_member_function_pointer<Type>::value)
00504   #define has_mbrfctnptr_type(Type)    assert(claim_mbrfctnptr(Type))
00505   #define has_nonmbrfctnptr_type(Type) assert(!claim_mbrfctnptr(Type))
00506 
00507   has_nonmbrfctnptr_type(f1);
00508   has_nonmbrfctnptr_type(f2);
00509   has_nonmbrfctnptr_type(f3);
00510   has_nonmbrfctnptr_type(void*);
00511   has_mbrfctnptr_type(mf1);
00512   has_mbrfctnptr_type(mf2);
00513   has_mbrfctnptr_type(mf3);
00514   has_mbrfctnptr_type(mf4);
00515   has_mbrfctnptr_type(cmf);
00516   has_nonmbrfctnptr_type(mp);
00517   has_nonmbrfctnptr_type(void);
00518   has_nonmbrfctnptr_type(test_abc1);
00519   has_nonmbrfctnptr_type(foo0_t);
00520   has_nonmbrfctnptr_type(int&);
00521   has_nonmbrfctnptr_type(const int&);
00522   has_nonmbrfctnptr_type(const int[2]);
00523   has_nonmbrfctnptr_type(const int[]);
00524   has_nonmbrfctnptr_type(void);
00525 
00526   #define claim_enum(Type)       (is_enum<Type>::value)
00527   #define has_enum_type(Type)    assert(claim_enum(Type))
00528   #define has_nonenum_type(Type) assert(!claim_enum(Type))
00529 
00530   has_nonenum_type(int);
00531   has_nonenum_type(long double);
00532   has_enum_type(enum_UDT);
00533   has_nonenum_type(int_convertible);
00534   has_nonenum_type(int&);
00535   has_nonenum_type(noncopyable);
00536   has_nonenum_type(void);
00537   has_nonenum_type(test_abc1);
00538   has_nonenum_type(foo0_t);
00539   has_nonenum_type(int&);
00540   has_nonenum_type(const int&);
00541   has_nonmbrfctnptr_type(const int[2]);
00542   has_nonmbrfctnptr_type(const int[]);
00543   has_nonmbrfctnptr_type(void);
00544 
00545   return 0;
00546 }

Generated on 15 Nov 2012 for CLHEP by  doxygen 1.4.7