CLHEP VERSION Reference Documentation
CLHEP Home Page
CLHEP Documentation
CLHEP Bug Reports
Main Page
Namespaces
Classes
Files
File List
File Members
Utility
test
testPrimaryTraits.cc
Go to the documentation of this file.
1
// ======================================================================
2
//
3
// Test basic functionality of primary type traits
4
//
5
// Author: W. E. Brown, 2010-03-24, adapted from the boost library's
6
// type_traits and related functionality whose internal attributions bear
7
// the following various notices:
8
//
9
// (C) Copyright John Maddock 2000.
10
// Distributed under the Boost Software License, Version 1.0.
11
// See http://www.boost.org/LICENSE_1_0.txt
12
//
13
// ======================================================================
14
15
16
#include "CLHEP/Utility/noncopyable.h"
17
#include "CLHEP/Utility/type_traits.h"
18
19
#include <cassert>
20
21
22
using namespace
CLHEP;
23
24
25
// define some test types:
26
27
enum
enum_UDT
{
one
,
two
,
three
};
28
struct
UDT
29
{
30
UDT
() { };
31
~UDT
() { };
32
UDT
(
const
UDT
&);
33
UDT
& operator=(
const
UDT
&);
34
int
i;
35
36
void
f1
();
37
int
f2
();
38
int
f3
(
int
);
39
int
f4(
int
,
float
);
40
};
41
42
typedef
void(*
f1
)();
43
typedef
int(*
f2
)(int);
44
typedef
int(*
f3
)(int, bool);
45
typedef
void (
UDT
::*
mf1
)();
46
typedef
int (
UDT
::*
mf2
)();
47
typedef
int (
UDT
::*
mf3
)(int);
48
typedef
int (
UDT
::*
mf4
)(int, float);
49
typedef
int (
UDT
::*
mp
);
50
typedef
int (
UDT
::*
cmf
)(int)
const
;
51
52
// cv-qualifiers applied to reference types should have no effect
53
54
// This is intentional:
55
// r_type and cr_type should be the same type
56
// but some compilers wrongly apply cv-qualifiers
57
// to reference types (this may generate a warning
58
// on some compilers):
59
60
typedef
int
&
r_type
;
61
#if ! defined(_MSC_VER)
62
typedef
const
r_type
cr_type
;
63
#endif // _MSC_VER
64
65
struct
POD_UDT
{
int
x; };
66
struct
empty_UDT
67
{
68
empty_UDT
() { };
69
empty_UDT
(
const
empty_UDT
&) { };
70
~empty_UDT
() { };
71
empty_UDT
&
operator=
(
const
empty_UDT
&){
return
*
this
; }
72
bool
operator==
(
const
empty_UDT
&)
const
73
{
return
true
; }
74
};
75
struct
empty_POD_UDT
76
{
77
bool
operator==
(
const
empty_POD_UDT
&)
const
78
{
return
true
; }
79
};
80
union
union_UDT
81
{
82
int
x;
83
double
y;
84
~union_UDT
() { }
85
};
86
union
POD_union_UDT
87
{
88
int
x;
89
double
y;
90
};
91
union
empty_union_UDT
92
{
93
~empty_union_UDT
() { }
94
};
95
union
empty_POD_union_UDT
{ };
96
97
struct
nothrow_copy_UDT
98
{
99
nothrow_copy_UDT
();
100
nothrow_copy_UDT
(
const
nothrow_copy_UDT
&)throw();
101
~
nothrow_copy_UDT
() { };
102
nothrow_copy_UDT
&
operator=
(
const
nothrow_copy_UDT
&){
return
*
this
; }
103
bool
operator==
(
const
nothrow_copy_UDT
&)
const
104
{
return
true
; }
105
};
106
107
struct
nothrow_assign_UDT
108
{
109
nothrow_assign_UDT
();
110
nothrow_assign_UDT
(
const
nothrow_assign_UDT
&);
111
~nothrow_assign_UDT
() { };
112
nothrow_assign_UDT
&
operator=
(
const
nothrow_assign_UDT
&)throw(){
return
*
this
; }
113
bool
operator==
(
const
nothrow_assign_UDT
&)
const
114
{
return
true
; }
115
};
116
117
struct
nothrow_construct_UDT
118
{
119
nothrow_construct_UDT
()throw();
120
nothrow_construct_UDT
(const
nothrow_construct_UDT
&);
121
~nothrow_construct_UDT() { };
122
nothrow_construct_UDT
&
operator=
(
const
nothrow_construct_UDT
&){
return
*
this
; }
123
bool
operator==
(
const
nothrow_construct_UDT
&)
const
124
{
return
true
; }
125
};
126
127
class
Base
{ };
128
129
class
Derived
:
public
Base
{ };
130
class
Derived2
:
public
Base
{ };
131
class
MultiBase
:
public
Derived
,
public
Derived2
{ };
132
class
PrivateBase
:
private
Base
{ };
133
134
class
NonDerived
{ };
135
136
enum
enum1
137
{
one_
,
two_
};
138
139
enum
enum2
140
{
three_
,
four_
};
141
142
struct
VB
143
{
virtual
~VB
() { }; };
144
145
struct
VD
:
VB
146
{
~VD
() { }; };
147
148
// struct non_pointer:
149
// used to verify that is_pointer does not return
150
// true for class types that implement operator void*()
151
152
struct
non_pointer
153
{
operator
void
*(){
return
this
;} };
154
struct
non_int_pointer
155
{
156
int
i;
157
operator
int
*(){
return
&i;}
158
};
159
struct
int_constructible
160
{
int_constructible
(
int
); };
161
struct
int_convertible
162
{
operator
int(); };
163
164
// struct non_empty:
165
// used to verify that is_empty does not emit
166
// spurious warnings or errors.
167
168
struct
non_empty
:
private
noncopyable
169
{
int
i; };
170
171
// abstract base classes:
172
struct
test_abc1
173
{
174
test_abc1
();
175
virtual
~
test_abc1
();
176
test_abc1
(
const
test_abc1
&);
177
test_abc1
& operator=(
const
test_abc1
&);
178
virtual
void
foo() = 0;
179
virtual
void
foo2() = 0;
180
};
181
182
struct
test_abc2
183
{
184
virtual
~
test_abc2
();
185
virtual
void
foo() = 0;
186
virtual
void
foo2() = 0;
187
};
188
189
struct
test_abc3
:
public
test_abc1
190
{
virtual
void
foo3() = 0; };
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
struct
polymorphic_derived2
:
polymorphic_base
204
{
virtual
void
method(); };
205
206
struct
virtual_inherit1
:
virtual
Base
{ };
207
struct
virtual_inherit2
:
virtual_inherit1
{ };
208
struct
virtual_inherit3
:
private
virtual
Base
{ };
209
struct
virtual_inherit4
:
virtual
noncopyable
{ };
210
struct
virtual_inherit5
:
virtual
int_convertible
{ };
211
struct
virtual_inherit6
:
virtual
Base
{
virtual
~
virtual_inherit6
()throw(); };
212
213
typedef
void
foo0_t
();
214
typedef
void
foo1_t
(
int
);
215
typedef
void
foo2_t
(
int
&,
double
);
216
typedef
void
foo3_t
(
int
&,
bool
,
int
,
int
);
217
typedef
void
foo4_t
(
int
,
bool
,
int
*,
int
[],
int
,
int
,
int
,
int
,
int
);
218
219
struct
trivial_except_construct
220
{
221
trivial_except_construct();
222
int
i;
223
};
224
225
struct
trivial_except_destroy
226
{
227
~
trivial_except_destroy
();
228
int
i;
229
};
230
231
struct
trivial_except_copy
232
{
233
trivial_except_copy
(
trivial_except_copy
const
&);
234
int
i;
235
};
236
237
struct
trivial_except_assign
238
{
239
trivial_except_assign
& operator=(
trivial_except_assign
const
&);
240
int
i;
241
};
242
243
template
<
class
T>
244
struct
wrap
245
{
246
T t;
247
int
j;
248
protected
:
249
wrap
();
250
wrap
(
const
wrap
&);
251
wrap
& operator=(
const
wrap
&);
252
};
253
254
255
struct
convertible_to_pointer
256
{
operator
char
*()
const
; };
257
258
259
typedef
const
double
(
UDT
::*
mp2
) ;
260
261
262
int
main
()
263
{
264
#define claim_void(Type) (is_void<Type>::value)
265
#define has_void_type(Type) assert(claim_void(Type))
266
#define has_nonvoid_type(Type) assert(!claim_void(Type))
267
268
has_void_type
(
void
);
269
has_void_type
(
void
const
);
270
has_void_type
(
void
volatile
);
271
has_void_type
(
void
const
volatile
);
272
273
has_nonvoid_type
(
void
*);
274
has_nonvoid_type
(
int
);
275
has_nonvoid_type
(
test_abc1
);
276
has_nonvoid_type
(
foo0_t
);
277
has_nonvoid_type
(
foo1_t
);
278
has_nonvoid_type
(
foo2_t
);
279
has_nonvoid_type
(
foo3_t
);
280
has_nonvoid_type
(
foo4_t
);
281
has_nonvoid_type
(incomplete_type);
282
283
#define claim_integral(Type) (is_integral<Type>::value)
284
#define has_integral_type(Type) assert(claim_integral(Type))
285
#define has_nonintegral_type(Type) assert(!claim_integral(Type))
286
287
has_integral_type
(
bool
);
288
has_integral_type
(
bool
const
);
289
has_integral_type
(
bool
volatile
);
290
has_integral_type
(
bool
const
volatile
);
291
292
has_integral_type
(
signed
char
);
293
has_integral_type
(
signed
char
const
);
294
has_integral_type
(
signed
char
volatile
);
295
has_integral_type
(
signed
char
const
volatile
);
296
has_integral_type
(
unsigned
char
);
297
has_integral_type
(
char
);
298
has_integral_type
(
unsigned
char
const
);
299
has_integral_type
(
char
const
);
300
has_integral_type
(
unsigned
char
volatile
);
301
has_integral_type
(
char
volatile
);
302
has_integral_type
(
unsigned
char
const
volatile
);
303
has_integral_type
(
char
const
volatile
);
304
305
has_integral_type
(
unsigned
short
);
306
has_integral_type
(
short
);
307
has_integral_type
(
unsigned
short
const
);
308
has_integral_type
(
short
const
);
309
has_integral_type
(
unsigned
short
volatile
);
310
has_integral_type
(
short
volatile
);
311
has_integral_type
(
unsigned
short
const
volatile
);
312
has_integral_type
(
short
const
volatile
);
313
314
has_integral_type
(
unsigned
int
);
315
has_integral_type
(
int
);
316
has_integral_type
(
unsigned
int
const
);
317
has_integral_type
(
int
const
);
318
has_integral_type
(
unsigned
int
volatile
);
319
has_integral_type
(
int
volatile
);
320
has_integral_type
(
unsigned
int
const
volatile
);
321
has_integral_type
(
int
const
volatile
);
322
323
has_integral_type
(
unsigned
long
);
324
has_integral_type
(
long
);
325
has_integral_type
(
unsigned
long
const
);
326
has_integral_type
(
long
const
);
327
has_integral_type
(
unsigned
long
volatile
);
328
has_integral_type
(
long
volatile
);
329
has_integral_type
(
unsigned
long
const
volatile
);
330
has_integral_type
(
long
const
volatile
);
331
332
has_nonintegral_type
(
void
);
333
has_nonintegral_type
(
float
);
334
has_nonintegral_type
(
UDT
);
335
has_nonintegral_type
(
test_abc1
);
336
has_nonintegral_type
(
empty_UDT
);
337
has_nonintegral_type
(
int
*);
338
has_nonintegral_type
(
int
&);
339
has_nonintegral_type
(
const
int
&);
340
has_nonintegral_type
(
int
[2]);
341
has_nonintegral_type
(
test_abc1
);
342
has_nonintegral_type
(
foo0_t
);
343
has_nonintegral_type
(
foo1_t
);
344
has_nonintegral_type
(
foo2_t
);
345
has_nonintegral_type
(
foo3_t
);
346
has_nonintegral_type
(
foo4_t
);
347
has_nonintegral_type
(incomplete_type);
348
349
#define claim_floating(Type) (is_floating_point<Type>::value)
350
#define has_floating_type(Type) assert(claim_floating(Type))
351
#define has_nonfloating_type(Type) assert(!claim_floating(Type))
352
353
has_floating_type
(
float
);
354
has_floating_type
(
float
const
);
355
has_floating_type
(
float
volatile
);
356
has_floating_type
(
float
const
volatile
);
357
358
has_floating_type
(
double
);
359
has_floating_type
(
double
const
);
360
has_floating_type
(
double
volatile
);
361
has_floating_type
(
double
const
volatile
);
362
363
has_floating_type
(
long
double
);
364
has_floating_type
(
long
double
const
);
365
has_floating_type
(
long
double
volatile
);
366
has_floating_type
(
long
double
const
volatile
);
367
368
has_nonfloating_type
(
void
);
369
has_nonfloating_type
(
int
);
370
has_nonfloating_type
(
UDT
);
371
has_nonfloating_type
(
test_abc1
);
372
has_nonfloating_type
(
empty_UDT
);
373
has_nonfloating_type
(
float
*);
374
has_nonfloating_type
(
float
&);
375
has_nonfloating_type
(
const
float
&);
376
has_nonfloating_type
(
float
[2]);
377
378
has_nonfloating_type
(
test_abc1
);
379
has_nonfloating_type
(
foo0_t
);
380
has_nonfloating_type
(
foo1_t
);
381
has_nonfloating_type
(
foo2_t
);
382
has_nonfloating_type
(
foo3_t
);
383
has_nonfloating_type
(
foo4_t
);
384
has_nonfloating_type
(incomplete_type);
385
386
#define claim_array(Type) (is_array<Type>::value)
387
#define has_array_type(Type) assert(claim_array(Type))
388
#define has_nonarray_type(Type) assert(!claim_array(Type))
389
390
has_nonarray_type
(
int
);
391
has_nonarray_type
(
int
*);
392
has_nonarray_type
(
const
int
*);
393
has_nonarray_type
(
const
volatile
int
*);
394
has_nonarray_type
(
int
*
const
);
395
has_nonarray_type
(
const
int
*
volatile
);
396
has_nonarray_type
(
const
volatile
int
*
const
);
397
has_array_type
(
int
[2]);
398
has_array_type
(
const
int
[2]);
399
has_array_type
(
const
volatile
int
[2]);
400
has_array_type
(
int
[2][3]);
401
has_array_type
(
UDT
[2]);
402
has_nonarray_type
(
int
(&)[2]);
403
has_nonarray_type
(
f1
);
404
has_nonarray_type
(
void
);
405
has_nonarray_type
(
test_abc1
);
406
has_nonarray_type
(
convertible_to_pointer
);
407
has_nonarray_type
(
test_abc1
);
408
has_nonarray_type
(
foo0_t
);
409
has_nonarray_type
(incomplete_type);
410
411
#define claim_ptr(Type) (is_pointer<Type>::value)
412
#define has_ptr_type(Type) assert(claim_ptr(Type))
413
#define has_nonptr_type(Type) assert(!claim_ptr(Type))
414
415
has_nonptr_type
(
int
);
416
has_nonptr_type
(
int
&);
417
has_ptr_type
(
int
*);
418
has_ptr_type
(
const
int
*);
419
has_ptr_type
(
volatile
int
*);
420
has_ptr_type
(
non_pointer
*);
421
has_ptr_type
(
int
*
const
);
422
has_ptr_type
(
int
*
volatile
);
423
has_ptr_type
(
int
*
const
volatile
);
424
has_nonptr_type
(
non_pointer
);
425
has_nonptr_type
(
int
*&);
426
has_nonptr_type
(
int
(&)[2]);
427
has_nonptr_type
(
int
[2]);
428
has_nonptr_type
(
char
[
sizeof
(
void
*)]);
429
has_nonptr_type
(
void
);
430
431
has_ptr_type
(
f1
);
432
has_ptr_type
(
f2
);
433
has_ptr_type
(
f3
);
434
has_nonptr_type
(
mf1
);
435
has_nonptr_type
(
mf2
);
436
has_nonptr_type
(
mf3
);
437
has_nonptr_type
(
mf4
);
438
has_nonptr_type
(
test_abc1
);
439
440
has_nonptr_type
(
foo0_t
);
441
has_nonptr_type
(
foo1_t
);
442
has_nonptr_type
(
foo2_t
);
443
has_nonptr_type
(
foo3_t
);
444
has_nonptr_type
(
foo4_t
);
445
446
has_nonptr_type
(
test_abc1
);
447
448
#define claim_lref(Type) (is_lvalue_reference<Type>::value)
449
#define has_lref_type(Type) assert(claim_lref(Type))
450
#define has_nonlref_type(Type) assert(!claim_lref(Type))
451
452
#define claim_ref(Type) (is_reference<Type>::value)
453
#define has_ref_type(Type) assert(claim_ref(Type))
454
#define has_nonref_type(Type) assert(!claim_ref(Type))
455
456
#define lref(Type) has_lref_type(Type); has_ref_type(Type);
457
#define nonref(Type) has_nonlref_type(Type); has_nonref_type(Type);
458
459
lref
(
int
&);
460
lref
(
const
int
&);
461
lref
(
volatile
int
&);
462
lref
(
const
volatile
int
&);
463
lref
(
r_type
);
464
#if ! defined(_MSC_VER)
465
lref
(
cr_type
);
466
#endif // _MSC_VER
467
lref
(
UDT
&);
468
lref
(
const
UDT
&);
469
lref
(
volatile
UDT
&);
470
lref
(
const
volatile
UDT
&);
471
lref
(
int
(&)(
int
));
472
lref
(
int
(&)[2]);
473
474
nonref
(
int
[2]);
475
nonref
(
const
int
[2]);
476
nonref
(
volatile
int
[2]);
477
nonref
(
const
volatile
int
[2]);
478
nonref
(
bool
);
479
nonref
(
void
);
480
nonref
(
test_abc1
);
481
nonref
(
foo0_t
);
482
nonref
(incomplete_type);
483
484
#define claim_mbrobjptr(Type) (is_member_object_pointer<Type>::value)
485
#define has_mbrobjptr_type(Type) assert(claim_mbrobjptr(Type))
486
#define has_nonmbrobjptr_type(Type) assert(!claim_mbrobjptr(Type))
487
488
has_nonmbrobjptr_type
(
f1
);
489
has_nonmbrobjptr_type
(
f2
);
490
has_nonmbrobjptr_type
(
f3
);
491
has_nonmbrobjptr_type
(
void
*);
492
has_nonmbrobjptr_type
(
mf1
);
493
has_nonmbrobjptr_type
(
mf2
);
494
has_nonmbrobjptr_type
(
mf3
);
495
has_nonmbrobjptr_type
(
mf4
);
496
has_nonmbrobjptr_type
(
cmf
);
497
has_mbrobjptr_type
(
mp
);
498
has_mbrobjptr_type
(
mp2
);
499
has_nonmbrobjptr_type
(
void
);
500
has_nonmbrobjptr_type
(
test_abc1
);
501
has_nonmbrobjptr_type
(
foo0_t
);
502
503
#define claim_mbrfctnptr(Type) (is_member_function_pointer<Type>::value)
504
#define has_mbrfctnptr_type(Type) assert(claim_mbrfctnptr(Type))
505
#define has_nonmbrfctnptr_type(Type) assert(!claim_mbrfctnptr(Type))
506
507
has_nonmbrfctnptr_type
(
f1
);
508
has_nonmbrfctnptr_type
(
f2
);
509
has_nonmbrfctnptr_type
(
f3
);
510
has_nonmbrfctnptr_type
(
void
*);
511
has_mbrfctnptr_type
(
mf1
);
512
has_mbrfctnptr_type
(
mf2
);
513
has_mbrfctnptr_type
(
mf3
);
514
has_mbrfctnptr_type
(
mf4
);
515
has_mbrfctnptr_type
(
cmf
);
516
has_nonmbrfctnptr_type
(
mp
);
517
has_nonmbrfctnptr_type
(
void
);
518
has_nonmbrfctnptr_type
(
test_abc1
);
519
has_nonmbrfctnptr_type
(
foo0_t
);
520
has_nonmbrfctnptr_type
(
int
&);
521
has_nonmbrfctnptr_type
(
const
int
&);
522
has_nonmbrfctnptr_type
(
const
int
[2]);
523
has_nonmbrfctnptr_type
(
const
int
[]);
524
has_nonmbrfctnptr_type
(
void
);
525
526
#define claim_enum(Type) (is_enum<Type>::value)
527
#define has_enum_type(Type) assert(claim_enum(Type))
528
#define has_nonenum_type(Type) assert(!claim_enum(Type))
529
530
has_nonenum_type
(
int
);
531
has_nonenum_type
(
long
double
);
532
has_enum_type
(
enum_UDT
);
533
has_nonenum_type
(
int_convertible
);
534
has_nonenum_type
(
int
&);
535
has_nonenum_type
(
noncopyable
);
536
has_nonenum_type
(
void
);
537
has_nonenum_type
(
test_abc1
);
538
has_nonenum_type
(
foo0_t
);
539
has_nonenum_type
(
int
&);
540
has_nonenum_type
(
const
int
&);
541
has_nonmbrfctnptr_type
(
const
int
[2]);
542
has_nonmbrfctnptr_type
(
const
int
[]);
543
has_nonmbrfctnptr_type
(
void
);
544
545
return
0;
546
}
Generated on Mon May 6 2013 04:04:11 for CLHEP by
1.8.1.2