From f6f2c78d9db37228baa9d1f9926b0c120e24016f Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Tue, 9 Jul 2024 12:12:56 +0100 Subject: [PATCH] libstdc++: Define C++26 member visit for std::variant [PR110356] Implement the std::variant changes from P2637R3. libstdc++-v3/ChangeLog: PR libstdc++/110356 * include/bits/version.def (variant): Update for C++26. * include/bits/version.h: Regenerate. * include/std/variant (variant::visit): New member functions. * testsuite/20_util/variant/visit.cc: Check second alternative. * testsuite/20_util/variant/visit_member.cc: New test. --- libstdc++-v3/include/bits/version.def | 5 + libstdc++-v3/include/bits/version.h | 7 +- libstdc++-v3/include/std/variant | 46 +++++++ .../testsuite/20_util/variant/visit.cc | 5 +- .../testsuite/20_util/variant/visit_member.cc | 117 ++++++++++++++++++ 5 files changed, 178 insertions(+), 2 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/variant/visit_member.cc diff --git a/libstdc++-v3/include/bits/version.def b/libstdc++-v3/include/bits/version.def index bcb33c18aa4d..806f1e9549be 100644 --- a/libstdc++-v3/include/bits/version.def +++ b/libstdc++-v3/include/bits/version.def @@ -478,6 +478,11 @@ ftms = { ftms = { name = variant; + values = { + v = 202306; + cxxmin = 26; + extra_cond = "__cpp_concepts >= 202002L && __cpp_constexpr >= 201811L && __cpp_explicit_this_parameter"; + }; values = { v = 202106; cxxmin = 20; diff --git a/libstdc++-v3/include/bits/version.h b/libstdc++-v3/include/bits/version.h index 4d1af34bf8d5..e8ca0faf5dcd 100644 --- a/libstdc++-v3/include/bits/version.h +++ b/libstdc++-v3/include/bits/version.h @@ -529,7 +529,12 @@ #undef __glibcxx_want_type_trait_variable_templates #if !defined(__cpp_lib_variant) -# if (__cplusplus >= 202002L) && (__cpp_concepts >= 202002L && __cpp_constexpr >= 201811L) +# if (__cplusplus > 202302L) && (__cpp_concepts >= 202002L && __cpp_constexpr >= 201811L && __cpp_explicit_this_parameter) +# define __glibcxx_variant 202306L +# if defined(__glibcxx_want_all) || defined(__glibcxx_want_variant) +# define __cpp_lib_variant 202306L +# endif +# elif (__cplusplus >= 202002L) && (__cpp_concepts >= 202002L && __cpp_constexpr >= 201811L) # define __glibcxx_variant 202106L # if defined(__glibcxx_want_all) || defined(__glibcxx_want_variant) # define __cpp_lib_variant 202106L diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant index 3a23d9bc66d0..d0f7bd0242f8 100644 --- a/libstdc++-v3/include/std/variant +++ b/libstdc++-v3/include/std/variant @@ -1390,6 +1390,12 @@ namespace __detail::__variant constexpr __detail::__variant::__visit_result_t<_Visitor, _Variants...> visit(_Visitor&&, _Variants&&...); +#if __cplusplus > 201703L + template + constexpr _Res + visit(_Visitor&&, _Variants&&...); +#endif + template _GLIBCXX20_CONSTEXPR inline enable_if_t<(is_move_constructible_v<_Types> && ...) @@ -1758,6 +1764,46 @@ namespace __detail::__variant }, __rhs); } +#if __cpp_lib_variant >= 202306L // >= C++26 + // [variant.visit], visitation + + /** Simple visitation for a single variant + * + * To visit a single variant you can use `var.visit(visitor)` + * instead of `std::visit(visitor, var)`. + * + * @since C++26 + */ + template + constexpr decltype(auto) + visit(this _Self&& __self, _Visitor&& __vis) + { + using _CVar = __conditional_t>, + const variant, variant>; + using _Var = __conditional_t, + _CVar&&, _CVar&>; + return std::visit(std::forward<_Visitor>(__vis), (_Var)__self); + } + + /** Simple visitation for a single variant, with explicit return type + * + * To visit a single variant you can use `var.visit(visitor)` + * instead of `std::visit(visitor, var)`. + * + * @since C++26 + */ + template + constexpr _Res + visit(this _Self&& __self, _Visitor&& __vis) + { + using _CVar = __conditional_t>, + const variant, variant>; + using _Var = __conditional_t, + _CVar&&, _CVar&>; + return std::visit<_Res>(std::forward<_Visitor>(__vis), (_Var)__self); + } +#endif + private: template friend constexpr decltype(auto) diff --git a/libstdc++-v3/testsuite/20_util/variant/visit.cc b/libstdc++-v3/testsuite/20_util/variant/visit.cc index 7f79e6107aba..6edc7d7c0283 100644 --- a/libstdc++-v3/testsuite/20_util/variant/visit.cc +++ b/libstdc++-v3/testsuite/20_util/variant/visit.cc @@ -18,7 +18,7 @@ // { dg-do run { target c++17 } } #include -#include +#include // reference_wrapper #include // N.B. there are more std::visit tests in ./compile.cc and ./run.cc @@ -84,6 +84,9 @@ test02() // Visit should not need arguments to be copyable: int res = std::visit(f, v); VERIFY( res == 1 ); + v.emplace(); + res = std::visit(f, v); + VERIFY( res == 0 ); } int diff --git a/libstdc++-v3/testsuite/20_util/variant/visit_member.cc b/libstdc++-v3/testsuite/20_util/variant/visit_member.cc new file mode 100644 index 000000000000..90332b94c72b --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/variant/visit_member.cc @@ -0,0 +1,117 @@ +// Copyright (C) 2019-2024 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(); +} -- 2.47.2