// Copyright (C) 2019-2025 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// .
// { dg-do run { target c++26 } }
#include
#if __cpp_lib_variant < 202306L
# error __cpp_lib_variant has the wrong value in
#endif
#include // reference_wrapper
#include
void
test01()
{
// Verify that visitation uses INVOKE and supports a pointer-to-member.
struct X { int n; };
using V = std::variant>;
struct Derv : private V {
using V::V;
using V::visit;
};
Derv v{X{1}};
static_assert( std::is_same_v );
// Verify that constness and value category are correctly forwarded.
std::variant v2{1};
auto id = [](T&& t) -> T&& { return std::forward(t); };
static_assert( std::is_same_v );
static_assert( std::is_same_v );
const auto& vc = v2;
static_assert( std::is_same_v );
static_assert( std::is_same_v );
static_assert( std::is_same_v(id)), void> );
}
void
test02()
{
struct NoCopy
{
NoCopy() { }
NoCopy(const NoCopy&) = delete;
NoCopy(NoCopy&&) = delete;
~NoCopy() { }
int operator()(int i) { return i; }
int operator()(const NoCopy&) { return 100; }
};
std::variant v{10};
NoCopy f;
// Visit should not need arguments to be copyable:
int res = v.visit(f);
VERIFY( res == 10 );
v.emplace();
res = v.visit(f);
VERIFY( res == 100 );
res = v.visit(f);
VERIFY( res == 1 );
}
void
test03()
{
// Verify that member visit can access the variant as a private base class.
struct Derived : private std::variant
{
using variant::visit;
};
Derived d;
int i = d.visit([](int& x) { return --x; });
VERIFY( i == -1 );
unsigned u = d.visit([](int x) { return x; });
VERIFY( u == -1u );
}
void
test04()
{
struct A { char a = 'a'; };
struct B { char b = 'b'; };
struct C { char c = 'c'; };
auto f = [](auto x) { return B{}; };
using State = std::variant;
State state = A{};
auto res = std::move(state).visit(f);
// Verify that visit only matches the explicit return type overload.
static_assert( std::is_same_v );
}
int
main()
{
test01();
test02();
test03();
test04();
}