CLHEP VERSION Reference Documentation
CLHEP Home Page
CLHEP Documentation
CLHEP Bug Reports
Main Page
Namespaces
Classes
Files
File List
File Members
Utility
test
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
{
75
~empty_union_UDT
() { }
76
};
77
union
empty_POD_union_UDT
{ };
78
79
struct
nothrow_copy_UDT
80
{
81
nothrow_copy_UDT
();
82
nothrow_copy_UDT
(
const
nothrow_copy_UDT
&)throw();
83
~
nothrow_copy_UDT
() { };
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
{
91
nothrow_assign_UDT
();
92
nothrow_assign_UDT
(
const
nothrow_assign_UDT
&);
93
~nothrow_assign_UDT
() { };
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
99
struct
nothrow_construct_UDT
100
{
101
nothrow_construct_UDT
()throw();
102
nothrow_construct_UDT
(const
nothrow_construct_UDT
&);
103
~nothrow_construct_UDT() { };
104
nothrow_construct_UDT
&
operator=
(
const
nothrow_construct_UDT
&){
return
*
this
; }
105
bool
operator==
(
const
nothrow_construct_UDT
&)
const
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
{
120
one_
,
two_
121
};
122
123
enum
enum2
124
{
125
three_
,
four_
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
200
struct
polymorphic_derived1
:
polymorphic_base
201
{
202
};
203
204
struct
polymorphic_derived2
:
polymorphic_base
205
{
206
virtual
void
method();
207
};
208
209
struct
virtual_inherit1
:
virtual
Base
{ };
210
struct
virtual_inherit2
:
virtual_inherit1
{ };
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
222
struct
trivial_except_construct
223
{
224
trivial_except_construct();
225
int
i;
226
};
227
228
struct
trivial_except_destroy
229
{
230
~
trivial_except_destroy
();
231
int
i;
232
};
233
234
struct
trivial_except_copy
235
{
236
trivial_except_copy
(
trivial_except_copy
const
&);
237
int
i;
238
};
239
240
struct
trivial_except_assign
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 >
259
struct
convertible_from
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
273
does_not_convert
(
NonDerived
,
Base
);
274
275
does_convert
(
Base
,
Base
);
276
does_convert
(
Derived
,
Base
);
277
does_convert
(
Derived
,
Derived
);
278
does_not_convert
(
Base
,
Derived
);
279
280
does_convert
(
Derived
&,
Base
&);
281
does_convert
(
const
Derived
&,
const
Base
&);
282
does_not_convert
(
Base
&,
Derived
&);
283
does_not_convert
(
const
Base
&,
const
Derived
&);
284
285
does_convert
(
Derived
*,
Base
*);
286
does_convert
(
const
Derived
*,
const
Base
*);
287
does_not_convert
(
Base
*,
Derived
*);
288
does_not_convert
(
const
Base
*,
const
Derived
*);
289
290
does_convert
(
polymorphic_derived1
,
polymorphic_base
);
291
does_convert
(
polymorphic_derived2
,
polymorphic_base
);
292
does_not_convert
(
polymorphic_base
,
polymorphic_derived1
);
293
does_not_convert
(
polymorphic_base
,
polymorphic_derived2
);
294
295
does_convert
(
virtual_inherit2
,
virtual_inherit1
);
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
*);
320
does_not_convert
(
non_pointer
,
int
*);
321
does_convert
(
non_int_pointer
,
int
*);
322
does_convert
(
non_int_pointer
,
void
*);
323
324
does_not_convert
(
test_abc1
&,
test_abc2
&);
325
does_not_convert
(
test_abc1
&,
int_constructible
);
326
does_not_convert
(
int_constructible
,
test_abc1
&);
327
328
does_not_convert
(
test_abc1
&,
test_abc2
);
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
336
does_convert
(
int
,
int_constructible
);
337
does_convert
(
float
,
convertible_from<float>
);
338
does_convert
(
float
,
convertible_from<float const&>
);
339
does_convert
(
float
,
convertible_from<float&>
);
340
341
does_convert
(
float
,
convertible_from<char>
);
342
does_convert
(
float
,
convertible_from<char const&>
);
343
does_not_convert
(
float
,
convertible_from<char&>
);
344
345
does_convert
(
char
,
convertible_from<char>
);
346
does_convert
(
char
,
convertible_from<char const&>
);
347
does_convert
(
char
,
convertible_from<char&>
);
348
349
does_convert
(
float
&,
convertible_from<float>
);
350
does_convert
(
float
const
&,
convertible_from<float>
);
351
does_convert
(
float
&,
convertible_from<float&>
);
352
does_convert
(
float
const
&,
convertible_from<float const&>
);
353
does_convert
(
float
&,
convertible_from<float const&>
);
354
#endif // compiler
355
356
return
0;
357
}
Generated on Sun Jun 17 2012 08:08:27 for CLHEP by
1.8.1.1