-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer_union_test.cpp
More file actions
105 lines (98 loc) · 2.34 KB
/
pointer_union_test.cpp
File metadata and controls
105 lines (98 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "pointer_union.hpp"
#include <cassert>
int main(const int argc, const char** argv) {
using namespace std;
using namespace com_masaers;
int i = 2;
const int ci = 5;
double d = 3.14;
{
ptrunion<int, const int, double> pu(&i);
assert( (int*)pu == &i);
assert( (void*)pu == &i);
assert( (int*)pu != NULL);
assert((const int*)pu == NULL);
assert( (double*)pu == NULL);
assert( pu.is<int>());
assert(! pu.is<const int>());
assert(! pu.is<double>());
assert( pu.is<void>());
pu = &ci;
assert((const int*)pu == &ci);
assert( (void*)pu == &ci);
assert( (int*)pu == NULL);
assert((const int*)pu != NULL);
assert( (double*)pu == NULL);
assert(! pu.is<int>());
assert( pu.is<const int>());
assert(! pu.is<double>());
assert( pu.is<void>());
pu = &d;
assert( (double*)pu == &d);
assert( (void*)pu == &d);
assert( (int*)pu == NULL);
assert((const int*)pu == NULL);
assert( (double*)pu != NULL);
assert(! pu.is<int>());
assert(! pu.is<const int>());
assert( pu.is<double>());
assert( pu.is<void>());
}
{
ptrunion<int, double> pu(&d);
pu = &ci;
assert(pu.is<void>());
assert((void*)pu == &ci);
}
{
typedef ptrunion<int, const int, double> pu_type;
pu_type pu(&i);
switch (pu.id()) {
case pu_type::id_of<int>::value : {
break; }
case pu_type::id_of<const int>::value : {
assert(false);
break; }
case pu_type::id_of<double>::value : {
assert(false);
break; }
case pu_type::id_of<void>::value : {
assert(false);
break; }
};
}
{
typedef ptrunion<int, const int, double> pu_type;
pu_type pu(&ci);
switch (pu.id()) {
case pu_type::id_of<const int>::value : {
break; }
default : {
assert(false);
break; }
};
}
{
typedef ptrunion<int, const int, double> pu_type;
pu_type pu(&d);
switch (pu.id()) {
case pu_type::id_of<double>::value : {
break; }
default : {
assert(false);
break; }
};
}
{
typedef ptrunion<int, const int, double> pu_type;
pu_type pu;
switch (pu.id()) {
case pu_type::id_of<void>::value : {
break; }
default : {
assert(false);
break; }
};
}
return EXIT_SUCCESS;
}