| 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++20 } } |
| 19 |
|
| 20 |
#include <variant> |
| 21 |
#include <testsuite_hooks.h> |
| 22 |
|
| 23 |
void |
| 24 |
test01() |
| 25 |
{ |
| 26 |
struct Visitor |
| 27 |
{ |
| 28 |
int operator()(int, void*) const { return 0; } |
| 29 |
int operator()(char& c, void* p) const { return &c == p; } |
| 30 |
int operator()(int i, const char* s) const { return s[i] == '\0'; } |
| 31 |
int operator()(char c, const char* s) const { return c == *s; } |
| 32 |
}; |
| 33 |
|
| 34 |
std::variant<int, char> v1{'c'}; |
| 35 |
std::variant<void*, const char*> v2{"chars"}; |
| 36 |
|
| 37 |
auto res = std::visit<bool>(Visitor{}, v1, v2); |
| 38 |
static_assert(std::is_same_v<decltype(res), bool>); |
| 39 |
VERIFY( res == true ); |
| 40 |
|
| 41 |
static_assert(std::is_void_v<decltype(std::visit<void>(Visitor{}, v1, v2))>); |
| 42 |
} |
| 43 |
|
| 44 |
void test02() |
| 45 |
{ |
| 46 |
struct Visitor |
| 47 |
{ |
| 48 |
int operator()(double) {return 42;} |
| 49 |
double operator()(int) {return 0.02;} |
| 50 |
}; |
| 51 |
std::variant<int, double> v; |
| 52 |
std::visit<int>(Visitor(), v); |
| 53 |
std::visit<const void>(Visitor(), v); |
| 54 |
} |
| 55 |
|
| 56 |
void test03() |
| 57 |
{ |
| 58 |
// PR libstdc++/106589 - visit<void> rejects lambdas that do not return void |
| 59 |
auto visitor = []{ return 0; }; |
| 60 |
std::visit<void>(visitor); |
| 61 |
std::visit<void>(static_cast<int(*)()>(visitor)); |
| 62 |
} |
| 63 |
|
| 64 |
int |
| 65 |
main() |
| 66 |
{ |
| 67 |
test01(); |
| 68 |
test02(); |
| 69 |
test03(); |
| 70 |
} |