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();
103  ~nothrow_construct_UDT() { };
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 {
224  trivial_except_construct();
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 }
void foo1_t(int)
void(UDT::* mf1)()
void foo0_t()
bool operator==(const nothrow_construct_UDT &) const
bool operator==(const nothrow_assign_UDT &) const
int(UDT::* mf4)(int, float)
int(UDT::* mf3)(int)
nothrow_copy_UDT & operator=(const nothrow_copy_UDT &)
empty_UDT(const empty_UDT &)
bool operator==(const nothrow_copy_UDT &) const
bool operator==(const empty_POD_UDT &) const
void foo2_t(int &, double)
void foo3_t(int &, bool, int, int)
void foo4_t(int, bool, int *, int[], int, int, int, int, int)
nothrow_construct_UDT & operator=(const nothrow_construct_UDT &)
nothrow_assign_UDT & operator=(const nothrow_assign_UDT &)
void(* f1)()
intUDT::* mp
int(UDT::* mf2)()
#define does_convert(From, To)
int(UDT::* cmf)(int) const
empty_UDT & operator=(const empty_UDT &)
virtual ~VB()
#define does_not_convert(From, To)
bool operator==(const empty_UDT &) const
enum_UDT
int(* f2)(int)
int(* f3)(int, bool)
int main()