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

testIsConvertible.cc
Go to the documentation of this file.
1 // ======================================================================
2 // -*- C++ -*-
3 // $Id: testIsConvertible.cc,v 1.2 2010/06/16 14:15:01 garren Exp $
4 // ---------------------------------------------------------------------------
5 // Test is_convertible type trait
6 //
7 // W. E. Brown, 2010-03-19
8 // based on work by John Maddock
9 // ======================================================================
10 
11 
12 #include <CLHEP/Utility/noncopyable.h>
13 #include <CLHEP/Utility/type_traits.h>
14 #include <cassert>
15 
16 
17 using namespace CLHEP;
18 
19 
20 // define some test types:
21 
22 enum enum_UDT{ one, two, three };
23 struct UDT
24 {
25  UDT() { };
26  ~UDT() { };
27  UDT(const UDT&);
28  UDT& operator=(const UDT&);
29  int i;
30 
31  void f1();
32  int f2();
33  int f3(int);
34  int f4(int, float);
35 };
36 
37 typedef void(*f1)();
38 typedef int(*f2)(int);
39 typedef int(*f3)(int, bool);
40 typedef void (UDT::*mf1)();
41 typedef int (UDT::*mf2)();
42 typedef int (UDT::*mf3)(int);
43 typedef int (UDT::*mf4)(int, float);
44 typedef int (UDT::*mp);
45 typedef int (UDT::*cmf)(int) const;
46 
47 struct POD_UDT { int x; };
48 struct empty_UDT
49 {
50  empty_UDT() { };
51  empty_UDT(const empty_UDT&) { };
52  ~empty_UDT() { };
53  empty_UDT& operator=(const empty_UDT&){ return *this; }
54  bool operator==(const empty_UDT&)const
55  { return true; }
56 };
57 struct empty_POD_UDT
58 {
59  bool operator==(const empty_POD_UDT&)const
60  { return true; }
61 };
62 union union_UDT
63 {
64  int x;
65  double y;
66  ~union_UDT() { }
67 };
68 union POD_union_UDT
69 {
70  int x;
71  double y;
72 };
73 union empty_union_UDT
74 {
76 };
77 union empty_POD_union_UDT { };
78 
79 struct nothrow_copy_UDT
80 {
82  nothrow_copy_UDT(const nothrow_copy_UDT&)throw();
84  nothrow_copy_UDT& operator=(const nothrow_copy_UDT&){ return *this; }
85  bool operator==(const nothrow_copy_UDT&)const
86  { return true; }
87 };
88 
89 struct nothrow_assign_UDT
90 {
94  nothrow_assign_UDT& operator=(const nothrow_assign_UDT&)throw(){ return *this; }
95  bool operator==(const nothrow_assign_UDT&)const
96  { return true; }
97 };
98 
100 {
101  nothrow_construct_UDT()throw();
106  { return true; }
107 };
108 
109 class Base { };
110 
111 class Derived : public Base { };
112 class Derived2 : public Base { };
113 class MultiBase : public Derived, public Derived2 { };
114 class PrivateBase : private Base { };
115 
116 class NonDerived { };
117 
118 enum enum1
119 {
121 };
122 
123 enum enum2
124 {
126 };
127 
128 struct VB
129 {
130  virtual ~VB() { };
131 };
132 
133 struct VD : VB
134 {
135  ~VD() { };
136 };
137 
138 // struct non_pointer:
139 // used to verify that is_pointer does not return
140 // true for class types that implement operator void*()
141 //
142 struct non_pointer
143 {
144  operator void*(){return this;}
145 };
146 struct non_int_pointer
147 {
148  int i;
149  operator int*(){return &i;}
150 };
151 struct int_constructible
152 {
153  int_constructible(int);
154 };
155 struct int_convertible
156 {
157  operator int();
158 };
159 //
160 // struct non_empty:
161 // used to verify that is_empty does not emit
162 // spurious warnings or errors.
163 //
164 struct non_empty : private noncopyable
165 {
166  int i;
167 };
168 //
169 // abstract base classes:
170 struct test_abc1
171 {
172  test_abc1();
173  virtual ~test_abc1();
174  test_abc1(const test_abc1&);
175  test_abc1& operator=(const test_abc1&);
176  virtual void foo() = 0;
177  virtual void foo2() = 0;
178 };
179 
180 struct test_abc2
181 {
182  virtual ~test_abc2();
183  virtual void foo() = 0;
184  virtual void foo2() = 0;
185 };
186 
187 struct test_abc3 : public test_abc1
188 {
189  virtual void foo3() = 0;
190 };
191 
192 struct incomplete_type;
193 
194 struct polymorphic_base
195 {
196  virtual ~polymorphic_base();
197  virtual void method();
198 };
199 
201 {
202 };
203 
205 {
206  virtual void method();
207 };
208 
209 struct virtual_inherit1 : virtual Base { };
211 struct virtual_inherit3 : private virtual Base { };
212 struct virtual_inherit4 : virtual noncopyable { };
213 struct virtual_inherit5 : virtual int_convertible { };
214 struct virtual_inherit6 : virtual Base { virtual ~virtual_inherit6()throw(); };
215 
216 typedef void foo0_t();
217 typedef void foo1_t(int);
218 typedef void foo2_t(int&, double);
219 typedef void foo3_t(int&, bool, int, int);
220 typedef void foo4_t(int, bool, int*, int[], int, int, int, int, int);
221 
223 {
225  int i;
226 };
227 
229 {
231  int i;
232 };
233 
234 struct trivial_except_copy
235 {
237  int i;
238 };
239 
241 {
242  trivial_except_assign& operator=(trivial_except_assign const&);
243  int i;
244 };
245 
246 template< typename T >
247 struct wrap
248 {
249  T t;
250  int j;
251 protected:
252  wrap();
253  wrap(const wrap&);
254  wrap& operator=(const wrap&);
255 };
256 
257 
258 template< typename T >
260 { convertible_from(T); };
261 
262 struct base2 { };
263 struct middle2 : virtual base2 { };
264 struct derived2 : middle2 { };
265 
266 
267 int main()
268 {
269  #define conversion_claim(From,To) (is_convertible<From,To>::value)
270  #define does_convert(From,To) assert(conversion_claim(From,To))
271  #define does_not_convert(From,To) assert(!conversion_claim(From,To))
272 
274 
279 
281  does_convert(const Derived&, const Base&);
283  does_not_convert(const Base&, const Derived&);
284 
286  does_convert(const Derived*, const Base*);
288  does_not_convert(const Base*, const Derived*);
289 
294 
296  does_convert(VD,VB);
297 
298  does_convert(void,void);
299  does_not_convert(void,float);
300  does_convert(float,void);
301  //does_convert(float,int);
302 
303  does_convert(enum1, int);
304 
305  does_not_convert(const int *, int*);
306  does_not_convert(const int&, int&);
307  does_not_convert(const int*, int[3]);
308  does_convert(const int&, int);
309  does_convert(int(&)[4], const int*);
310  does_convert(int(&)(int), int(*)(int));
311  does_convert(int *, const int*);
312  does_convert(int&, const int&);
313  does_convert(int[2], int*);
314  does_convert(int[2], const int*);
315  does_not_convert(const int[2], int*);
316  does_not_convert(int*, int[3]);
317  //does_convert(test_abc3, const test_abc1&);
318 
319  does_convert(non_pointer, void*);
323 
327 
329 
330  #if defined(__GNUC__) && ((__GNUC__ < 4) || (__GNUC_MINOR__ < 2))
331  // known to be defective
332  #elif defined(_MSC_VER) && (_MSC_VER <= 1400)
333  // known to be defective
334  #else
335  // let's give it a try
340 
344 
348 
350  does_convert(float const&,convertible_from<float> );
354  #endif // compiler
355 
356  return 0;
357 }
empty_POD_UDT
Definition: testCategories.cc:184
polymorphic_base
Definition: testCategories.cc:303
nothrow_assign_UDT::~nothrow_assign_UDT
~nothrow_assign_UDT()
Definition: testIsConvertible.cc:93
UDT::UDT
UDT()
Definition: testIsConvertible.cc:25
virtual_inherit2
Definition: testCategories.cc:316
empty_POD_UDT::operator==
bool operator==(const empty_POD_UDT &) const
Definition: testIsConvertible.cc:59
empty_union_UDT
Definition: testCategories.cc:200
trivial_except_assign
Definition: testCategories.cc:346
empty_union_UDT::~empty_union_UDT
~empty_union_UDT()
Definition: testIsConvertible.cc:75
test_abc1
Definition: testCategories.cc:281
UDT
Definition: testCategories.cc:137
mf1
void(UDT::* mf1)()
Definition: testIsConvertible.cc:40
mp
intUDT::* mp
Definition: testCategories.cc:158
one_
@ one_
Definition: testIsConvertible.cc:120
nothrow_copy_UDT::~nothrow_copy_UDT
~nothrow_copy_UDT()
Definition: testIsConvertible.cc:83
enum2
enum2
Definition: testCategories.cc:248
NonDerived
Definition: testCategories.cc:243
empty_UDT::operator==
bool operator==(const empty_UDT &) const
Definition: testIsConvertible.cc:54
mf4
int(UDT::* mf4)(int, float)
Definition: testIsConvertible.cc:43
cmf
int(UDT::* cmf)(int) const
Definition: testIsConvertible.cc:45
polymorphic_derived2
Definition: testCategories.cc:312
virtual_inherit6
Definition: testCategories.cc:320
int_constructible
Definition: testCategories.cc:268
VD::~VD
~VD()
Definition: testIsConvertible.cc:135
nothrow_construct_UDT
Definition: testCategories.cc:226
UDT::~UDT
~UDT()
Definition: testIsConvertible.cc:26
empty_UDT::empty_UDT
empty_UDT(const empty_UDT &)
Definition: testIsConvertible.cc:51
PrivateBase
Definition: testCategories.cc:241
main
int main()
Definition: testIsConvertible.cc:267
does_convert
#define does_convert(From, To)
two_
@ two_
Definition: testIsConvertible.cc:120
nothrow_assign_UDT::operator=
nothrow_assign_UDT & operator=(const nothrow_assign_UDT &)
Definition: testIsConvertible.cc:94
test_abc2
Definition: testCategories.cc:291
foo4_t
void foo4_t(int, bool, int *, int[], int, int, int, int, int)
Definition: testIsConvertible.cc:220
trivial_except_copy
Definition: testCategories.cc:340
empty_UDT::empty_UDT
empty_UDT()
Definition: testIsConvertible.cc:50
trivial_except_destroy
Definition: testCategories.cc:334
non_int_pointer
Definition: testCategories.cc:263
three_
@ three_
Definition: testIsConvertible.cc:125
polymorphic_derived1
Definition: testCategories.cc:309
derived2
Definition: testIsConvertible.cc:264
POD_union_UDT
Definition: testCategories.cc:195
foo0_t
void foo0_t()
Definition: testIsConvertible.cc:216
empty_UDT::~empty_UDT
~empty_UDT()
Definition: testIsConvertible.cc:52
union_UDT
Definition: testCategories.cc:189
nothrow_construct_UDT::operator=
nothrow_construct_UDT & operator=(const nothrow_construct_UDT &)
Definition: testIsConvertible.cc:104
base2
Definition: testIsConvertible.cc:262
CLHEP
Definition: ClhepVersion.h:13
Base
Definition: testCategories.cc:236
middle2
Definition: testIsConvertible.cc:263
union_UDT::~union_UDT
~union_UDT()
Definition: testIsConvertible.cc:66
f3
int(* f3)(int, bool)
Definition: testIsConvertible.cc:39
foo2_t
void foo2_t(int &, double)
Definition: testIsConvertible.cc:218
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
empty_UDT::operator=
empty_UDT & operator=(const empty_UDT &)
Definition: testIsConvertible.cc:53
int_convertible
Definition: testCategories.cc:270
nothrow_copy_UDT::operator=
nothrow_copy_UDT & operator=(const nothrow_copy_UDT &)
Definition: testIsConvertible.cc:84
empty_UDT
Definition: testCategories.cc:175
MultiBase
Definition: testCategories.cc:240
nothrow_assign_UDT::operator==
bool operator==(const nothrow_assign_UDT &) const
Definition: testIsConvertible.cc:95
VB
Definition: testCategories.cc:251
foo3_t
void foo3_t(int &, bool, int, int)
Definition: testIsConvertible.cc:219
trivial_except_construct
Definition: testCategories.cc:328
mf2
int(UDT::* mf2)()
Definition: testIsConvertible.cc:41
enum_UDT
enum_UDT
Definition: testCategories.cc:136
enum1
enum1
Definition: testCategories.cc:245
j
long j
Definition: JamesRandomSeeding.txt:28
two
@ two
Definition: testIsConvertible.cc:22
non_empty
Definition: testCategories.cc:277
f1
void(* f1)()
Definition: testIsConvertible.cc:37
VB::~VB
virtual ~VB()
Definition: testIsConvertible.cc:130
does_not_convert
#define does_not_convert(From, To)
foo1_t
void foo1_t(int)
Definition: testIsConvertible.cc:217
i
long i
Definition: JamesRandomSeeding.txt:27
VD
Definition: testCategories.cc:254
virtual_inherit4
Definition: testCategories.cc:318
Derived
Definition: testCategories.cc:238
nothrow_construct_UDT::operator==
bool operator==(const nothrow_construct_UDT &) const
Definition: testIsConvertible.cc:105
f2
int(* f2)(int)
Definition: testIsConvertible.cc:38
test_abc3
Definition: testCategories.cc:298
one
@ one
Definition: testIsConvertible.cc:22
nothrow_copy_UDT
Definition: testCategories.cc:206
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
mf3
int(UDT::* mf3)(int)
Definition: testIsConvertible.cc:42
four_
@ four_
Definition: testIsConvertible.cc:125
convertible_from
Definition: testIsConvertible.cc:259
POD_UDT
Definition: testCategories.cc:174
non_pointer
Definition: testCategories.cc:261
Derived2
Definition: testCategories.cc:239
three
@ three
Definition: testIsConvertible.cc:22
nothrow_copy_UDT::operator==
bool operator==(const nothrow_copy_UDT &) const
Definition: testIsConvertible.cc:85
virtual_inherit5
Definition: testCategories.cc:319