]>
Commit | Line | Data |
---|---|---|
99dee823 | 1 | // Copyright (C) 2019-2021 Free Software Foundation, Inc. |
8701cb5e JW |
2 | // |
3 | // This file is part of the GNU ISO C++ Library. This library is free | |
4 | // software; you can redistribute it and/or modify it under the | |
5 | // terms of the GNU General Public License as published by the | |
6 | // Free Software Foundation; either version 3, or (at your option) | |
7 | // any later version. | |
8 | ||
9 | // This library is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | ||
14 | // You should have received a copy of the GNU General Public License along | |
15 | // with this library; see the file COPYING3. If not see | |
16 | // <http://www.gnu.org/licenses/>. | |
17 | ||
18 | // { dg-options "-std=gnu++17" } | |
19 | // { dg-do run { target c++17 } } | |
20 | ||
21 | #include <variant> | |
22 | #include <functional> | |
23 | #include <testsuite_hooks.h> | |
24 | ||
25 | // N.B. there are more std::visit tests in ./compile.cc and ./run.cc | |
26 | ||
27 | void | |
28 | test01() | |
29 | { | |
30 | // Verify that visitation uses INVOKE and supports arbitrary callables. | |
31 | ||
32 | struct X | |
33 | { | |
34 | int sum(int i) const { return i + n; } | |
35 | int product(int i) const { return i * n; } | |
36 | int n; | |
37 | }; | |
38 | ||
39 | std::variant<X, X*, std::reference_wrapper<X>> vobj{X{1}}; | |
40 | int res = std::visit(&X::n, vobj); | |
41 | VERIFY( res == 1 ); | |
42 | ||
43 | std::variant<int, short> varg{2}; | |
44 | res = std::visit(&X::sum, vobj, varg); | |
45 | VERIFY( res == 3 ); | |
46 | ||
47 | X x{4}; | |
48 | vobj = &x; | |
49 | res = std::visit(&X::n, vobj); | |
50 | VERIFY( res == 4 ); | |
51 | ||
52 | varg.emplace<short>(5); | |
53 | res = std::visit(&X::sum, vobj, varg); | |
54 | VERIFY( res == 9 ); | |
55 | ||
56 | x.n = 6; | |
57 | res = std::visit(&X::product, vobj, varg); | |
58 | VERIFY( res == 30 ); | |
59 | ||
60 | vobj = std::ref(x); | |
61 | x.n = 7; | |
62 | res = std::visit(&X::n, vobj); | |
63 | VERIFY( res == 7 ); | |
64 | ||
65 | res = std::visit(&X::product, vobj, varg); | |
66 | VERIFY( res == 35 ); | |
67 | } | |
68 | ||
9d89b73c JW |
69 | void |
70 | test02() | |
71 | { | |
72 | struct NoCopy | |
73 | { | |
74 | NoCopy() { } | |
75 | NoCopy(const NoCopy&) = delete; | |
76 | NoCopy(NoCopy&&) = delete; | |
77 | ~NoCopy() { } | |
78 | ||
79 | int operator()(int i) { return i; } | |
80 | int operator()(const NoCopy&) { return 0; } | |
81 | }; | |
82 | ||
83 | std::variant<NoCopy, int> v{1}; | |
84 | NoCopy f; | |
85 | // Visit should not need arguments to be copyable: | |
86 | int res = std::visit(f, v); | |
87 | VERIFY( res == 1 ); | |
88 | } | |
89 | ||
8701cb5e JW |
90 | int |
91 | main() | |
92 | { | |
93 | test01(); | |
9d89b73c | 94 | test02(); |
8701cb5e | 95 | } |