回答編集履歴
1
.
test
CHANGED
@@ -6,27 +6,17 @@
|
|
6
6
|
public:
|
7
7
|
Holder(T *p) : val(p) {}
|
8
8
|
long long Get() const { return static_cast<long long>(*val); }
|
9
|
-
T*
|
9
|
+
T *val;
|
10
10
|
};
|
11
11
|
|
12
|
-
class EmptyHolder {
|
13
|
-
public:
|
14
|
-
EmptyHolder() = default;
|
15
|
-
long long Get() const { return -1; }
|
16
|
-
};
|
17
|
-
|
18
|
-
using TEST =
|
19
|
-
|
12
|
+
using TEST = std::variant<Holder<int>, Holder<char>, Holder<long long>>;
|
20
13
|
|
21
14
|
int main() {
|
22
15
|
int a = 1;
|
23
16
|
char b = 'c';
|
24
17
|
long long c = 10000;
|
25
18
|
|
26
|
-
TEST test[3];
|
27
|
-
test[
|
19
|
+
TEST test[3] = {&a, &b, &c};
|
28
|
-
test[1] = &b;
|
29
|
-
test[2] = &c;
|
30
20
|
for (int i = 0; i < 3; i++) {
|
31
21
|
std::visit([](auto &v) { std::cout << v.Get() << std::endl; }, test[i]);
|
32
22
|
}
|