回答編集履歴

1

.

2024/02/05 07:44

投稿

int32_t
int32_t

スコア21599

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* val;
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
- std::variant<EmptyHolder, Holder<int>, Holder<char>, Holder<long long>>;
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[0] = &a;
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
  }