-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cc
141 lines (94 loc) · 2.14 KB
/
main.cc
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#define BR std::cout << "--" << std::endl;
struct IA {
virtual ~IA() = default;
virtual int GetValue1() = 0;
static int Id() { return 0; }
static void PrintId() { std::cout << Id() << std::endl; }
};
struct IB : IA {
~IB() override = default;
virtual int GetValue2() = 0;
static int Id() { return 1; }
static void PrintId() { std::cout << Id() << std::endl; }
};
struct IC : IB {
~IC() override = default;
virtual int GetValue3() = 0;
static int Id() { return 2; }
static void PrintId() { std::cout << Id() << std::endl; }
};
class C : public IC {
public:
C() = default;
~C() override = default;
int GetValue1() override { return 1; }
int GetValue2() override { return 2; }
int GetValue3() override { return 3; }
};
struct ID: IA {
~ID() override = default;
};
struct IE {
virtual ~IE() = default;
};
template <class T>
void PrintId1() {
std::cout << T::Id() << std::endl;
}
template<class ...TParams>
void PrintId2()
{
(TParams::PrintId(), ...);
}
template<class ...TParams>
void PrintId3()
{
(PrintId1<TParams>(), ...);
}
void test1() {
PrintId1<IA>();
// 0
BR
PrintId2<IA, IB, IC>();
// 0
// 1
// 2
BR
PrintId3<IA, IB, IC>();
// 0
// 1
// 2
}
void PrintValue(IA* a) {
std::cout << "GetValue1() = " << a->GetValue1() << std::endl;
}
void PrintValue(IB* b) {
std::cout << "GetValue2() = " << b->GetValue2() << std::endl;
}
void PrintValue(IC* c) {
std::cout << "GetValue3() = " << c->GetValue3() << std::endl;
}
void PrintValue(ID* d) {
std::cout << "PrintValue for ID ptr" << std::endl;
}
void PrintValue(IE* e) {
std::cout << "PrintValue for IE ptr" << std::endl;
}
template<class ...TParams, class TClass>
void PrintAllValues(TClass *c)
{
(
(
dynamic_cast<TParams *>(c) ? PrintValue(dynamic_cast<TParams *>(c))
: static_cast<void>(0)
)
, ...
);
}
int main(int argc, char const * argv[]) {
IC *c = new C();
PrintAllValues<IA, IB, IC, ID, IE>(c);
delete c;
return 0;
}