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
17using namespace CLHEP;
18
19
20// define some test types:
21
23struct 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
37typedef void(*f1)();
38typedef int(*f2)(int);
39typedef int(*f3)(int, bool);
40typedef void (UDT::*mf1)();
41typedef int (UDT::*mf2)();
42typedef int (UDT::*mf3)(int);
43typedef int (UDT::*mf4)(int, float);
44typedef int (UDT::*mp);
45typedef int (UDT::*cmf)(int) const;
46
47struct POD_UDT { int x; };
48struct empty_UDT
49{
50 empty_UDT() { };
51 empty_UDT(const empty_UDT&) { };
53 empty_UDT& operator=(const empty_UDT&){ return *this; }
54 bool operator==(const empty_UDT&)const
55 { return true; }
56};
57struct empty_POD_UDT
58{
59 bool operator==(const empty_POD_UDT&)const
60 { return true; }
61};
62union union_UDT
63{
64 int x;
65 double y;
67};
68union POD_union_UDT
69{
70 int x;
71 double y;
72};
74{
76};
77union empty_POD_union_UDT { };
78
80{
85 bool operator==(const nothrow_copy_UDT&)const
86 { return true; }
87};
88
90{
94 nothrow_assign_UDT& operator=(const nothrow_assign_UDT&)throw(){ return *this; }
96 { return true; }
97};
98
100{
106 { return true; }
107};
108
109class Base { };
110
111class Derived : public Base { };
112class Derived2 : public Base { };
113class MultiBase : public Derived, public Derived2 { };
114class PrivateBase : private Base { };
115
116class NonDerived { };
117
122
127
128struct VB
129{
130 virtual ~VB() { };
131};
132
133struct 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//
142struct non_pointer
143{
144 operator void*(){return this;}
145};
146struct non_int_pointer
147{
148 int i;
149 operator int*(){return &i;}
150};
152{
154};
155struct 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//
164struct non_empty : private noncopyable
165{
166 int i;
167};
168//
169// abstract base classes:
170struct test_abc1
171{
173 virtual ~test_abc1();
176 virtual void foo() = 0;
177 virtual void foo2() = 0;
178};
179
180struct test_abc2
181{
182 virtual ~test_abc2();
183 virtual void foo() = 0;
184 virtual void foo2() = 0;
185};
186
187struct test_abc3 : public test_abc1
188{
189 virtual void foo3() = 0;
190};
191
192struct incomplete_type;
193
194struct polymorphic_base
195{
197 virtual void method();
198};
199
201{
202};
203
205{
206 virtual void method();
207};
208
209struct virtual_inherit1 : virtual Base { };
211struct virtual_inherit3 : private virtual Base { };
212struct virtual_inherit4 : virtual noncopyable { };
213struct virtual_inherit5 : virtual int_convertible { };
214struct virtual_inherit6 : virtual Base { virtual ~virtual_inherit6()throw(); };
215
216typedef void foo0_t();
217typedef void foo1_t(int);
218typedef void foo2_t(int&, double);
219typedef void foo3_t(int&, bool, int, int);
220typedef 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
235{
237 int i;
238};
239
241{
243 int i;
244};
245
246template< typename T >
247struct wrap
248{
249 T t;
250 int j;
251protected:
253 wrap(const wrap&);
255};
256
257
258template< typename T >
261
262struct base2 { };
263struct middle2 : virtual base2 { };
264struct derived2 : middle2 { };
265
266
267int 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
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
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
354 #endif // compiler
355
356 return 0;
357}
void f1()
int f3(int)
int f4(int, float)
UDT(const UDT &)
UDT & operator=(const UDT &)
int f2()
virtual ~VB()
bool operator==(const empty_POD_UDT &) const
empty_UDT & operator=(const empty_UDT &)
bool operator==(const empty_UDT &) const
empty_UDT(const empty_UDT &)
nothrow_assign_UDT(const nothrow_assign_UDT &)
nothrow_assign_UDT & operator=(const nothrow_assign_UDT &)
bool operator==(const nothrow_assign_UDT &) const
bool operator==(const nothrow_construct_UDT &) const
nothrow_construct_UDT & operator=(const nothrow_construct_UDT &)
nothrow_copy_UDT & operator=(const nothrow_copy_UDT &)
bool operator==(const nothrow_copy_UDT &) const
nothrow_copy_UDT(const nothrow_copy_UDT &)
virtual ~polymorphic_base()
virtual void method()
virtual void method()
virtual void foo()=0
virtual ~test_abc1()
test_abc1 & operator=(const test_abc1 &)
test_abc1(const test_abc1 &)
virtual void foo2()=0
virtual void foo()=0
virtual void foo2()=0
virtual ~test_abc2()
virtual void foo3()=0
trivial_except_assign & operator=(trivial_except_assign const &)
trivial_except_copy(trivial_except_copy const &)
virtual ~virtual_inherit6()
wrap(const wrap &)
wrap & operator=(const wrap &)
intUDT::* mp
int(UDT::* mf2)()
int(UDT::* cmf)(int) const
int(UDT::* mf4)(int, float)
#define does_not_convert(From, To)
int(* f2)(int)
int(* f3)(int, bool)
void(* f1)()
void foo1_t(int)
void foo0_t()
void foo3_t(int &, bool, int, int)
void foo2_t(int &, double)
void foo4_t(int, bool, int *, int[], int, int, int, int, int)
void(UDT::* mf1)()
int(UDT::* mf3)(int)
#define does_convert(From, To)
int main()