| 1 |
// Copyright (C) 2019-2025 Free Software Foundation, Inc. |
| 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-do run { target c++26 } } |
| 19 |
|
| 20 |
#include <variant> |
| 21 |
|
| 22 |
#if __cpp_lib_variant < 202306L |
| 23 |
# error __cpp_lib_variant has the wrong value in <variant> |
| 24 |
#endif |
| 25 |
|
| 26 |
#include <functional> // reference_wrapper |
| 27 |
#include <testsuite_hooks.h> |
| 28 |
|
| 29 |
void |
| 30 |
test01() |
| 31 |
{ |
| 32 |
// Verify that visitation uses INVOKE and supports a pointer-to-member. |
| 33 |
struct X { int n; }; |
| 34 |
using V = std::variant<X, X*, std::reference_wrapper<X>>; |
| 35 |
struct Derv : private V { |
| 36 |
using V::V; |
| 37 |
using V::visit; |
| 38 |
}; |
| 39 |
|
| 40 |
Derv v{X{1}}; |
| 41 |
static_assert( std::is_same_v<decltype(v.visit(&X::n)), int&> ); |
| 42 |
|
| 43 |
// Verify that constness and value category are correctly forwarded. |
| 44 |
std::variant<int> v2{1}; |
| 45 |
auto id = []<typename T>(T&& t) -> T&& { return std::forward<T>(t); }; |
| 46 |
static_assert( std::is_same_v<decltype(v2.visit(id)), int&> ); |
| 47 |
static_assert( std::is_same_v<decltype(std::move(v2).visit(id)), int&&> ); |
| 48 |
const auto& vc = v2; |
| 49 |
static_assert( std::is_same_v<decltype(vc.visit(id)), const int&> ); |
| 50 |
static_assert( std::is_same_v<decltype(std::move(vc).visit(id)), const int&&> ); |
| 51 |
|
| 52 |
static_assert( std::is_same_v<decltype(vc.visit<void>(id)), void> ); |
| 53 |
} |
| 54 |
|
| 55 |
void |
| 56 |
test02() |
| 57 |
{ |
| 58 |
struct NoCopy |
| 59 |
{ |
| 60 |
NoCopy() { } |
| 61 |
NoCopy(const NoCopy&) = delete; |
| 62 |
NoCopy(NoCopy&&) = delete; |
| 63 |
~NoCopy() { } |
| 64 |
|
| 65 |
int operator()(int i) { return i; } |
| 66 |
int operator()(const NoCopy&) { return 100; } |
| 67 |
}; |
| 68 |
|
| 69 |
std::variant<NoCopy, int> v{10}; |
| 70 |
NoCopy f; |
| 71 |
// Visit should not need arguments to be copyable: |
| 72 |
int res = v.visit(f); |
| 73 |
VERIFY( res == 10 ); |
| 74 |
v.emplace<NoCopy>(); |
| 75 |
res = v.visit(f); |
| 76 |
VERIFY( res == 100 ); |
| 77 |
res = v.visit<bool>(f); |
| 78 |
VERIFY( res == 1 ); |
| 79 |
} |
| 80 |
|
| 81 |
void |
| 82 |
test03() |
| 83 |
{ |
| 84 |
// Verify that member visit can access the variant as a private base class. |
| 85 |
struct Derived : private std::variant<int> |
| 86 |
{ |
| 87 |
using variant::visit; |
| 88 |
}; |
| 89 |
Derived d; |
| 90 |
int i = d.visit([](int& x) { return --x; }); |
| 91 |
VERIFY( i == -1 ); |
| 92 |
unsigned u = d.visit<unsigned>([](int x) { return x; }); |
| 93 |
VERIFY( u == -1u ); |
| 94 |
} |
| 95 |
|
| 96 |
void |
| 97 |
test04() |
| 98 |
{ |
| 99 |
struct A { char a = 'a'; }; |
| 100 |
struct B { char b = 'b'; }; |
| 101 |
struct C { char c = 'c'; }; |
| 102 |
auto f = [](auto x) { return B{}; }; |
| 103 |
using State = std::variant<A, B, C>; |
| 104 |
State state = A{}; |
| 105 |
auto res = std::move(state).visit<State>(f); |
| 106 |
// Verify that visit<R> only matches the explicit return type overload. |
| 107 |
static_assert( std::is_same_v<decltype(res), State> ); |
| 108 |
} |
| 109 |
|
| 110 |
int |
| 111 |
main() |
| 112 |
{ |
| 113 |
test01(); |
| 114 |
test02(); |
| 115 |
test03(); |
| 116 |
test04(); |
| 117 |
} |