diff mbox series

[4/6] libstdc++: Define C++26 member visit for std::variant [PR110356]

Message ID 20240716120436.2135312-4-jwakely@redhat.com
State New
Headers show
Series [v3,1/6] libstdc++: Handle encodings in localized chrono formatting [PR109162] | expand

Commit Message

Jonathan Wakely July 16, 2024, 11:56 a.m. UTC
Tested x86_64-linux.

-- >8 --

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              |  48 +++++++
 .../testsuite/20_util/variant/visit.cc        |   5 +-
 .../testsuite/20_util/variant/visit_member.cc | 117 ++++++++++++++++++
 5 files changed, 180 insertions(+), 2 deletions(-)
 create mode 100644 libstdc++-v3/testsuite/20_util/variant/visit_member.cc
diff mbox series

Patch

diff --git a/libstdc++-v3/include/bits/version.def b/libstdc++-v3/include/bits/version.def
index bcb33c18aa4..806f1e9549b 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 4d1af34bf8d..e8ca0faf5dc 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 3a23d9bc66d..154aab98812 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<typename _Res, typename _Visitor, typename... _Variants>
+    constexpr _Res
+    visit(_Visitor&&, _Variants&&...);
+#endif
+
   template<typename... _Types>
     _GLIBCXX20_CONSTEXPR
     inline enable_if_t<(is_move_constructible_v<_Types> && ...)
@@ -1758,6 +1764,48 @@  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<int = 0, typename _Self, typename _Visitor>
+	constexpr decltype(auto)
+	visit(this _Self&& __self, _Visitor&& __vis)
+	{
+	  using _CVar = __conditional_t<is_const_v<remove_reference_t<_Self>>,
+					const variant, variant>;
+	  using _Var = __conditional_t<is_rvalue_reference_v<_Self&&>,
+				       _CVar&&, _CVar&>;
+	  static_assert(is_same_v<__like_t<_Self, variant>, _Var>);
+	  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<R>(visitor)`
+       * instead of `std::visit<R>(visitor, var)`.
+       *
+       * @since C++26
+       */
+      template<typename _Res, typename _Self, typename _Visitor>
+	constexpr _Res
+	visit(this _Self&& __self, _Visitor&& __vis)
+	{
+	  using _CVar = __conditional_t<is_const_v<remove_reference_t<_Self>>,
+					const variant, variant>;
+	  using _Var = __conditional_t<is_rvalue_reference_v<_Self&&>,
+				       _CVar&&, _CVar&>;
+	  static_assert(is_same_v<__like_t<_Self, variant>, _Var>);
+	  return std::visit<_Res>(std::forward<_Visitor>(__vis), (_Var)__self);
+	}
+#endif
+
     private:
       template<size_t _Np, typename _Vp>
 	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 7f79e6107ab..6edc7d7c028 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 <variant>
-#include <functional>
+#include <functional> // reference_wrapper
 #include <testsuite_hooks.h>
 
 // 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<NoCopy>();
+  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 00000000000..90332b94c72
--- /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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++26 } }
+
+#include <variant>
+
+#if __cpp_lib_variant < 202306L
+# error __cpp_lib_variant has the wrong value in <variant>
+#endif
+
+#include <functional> // reference_wrapper
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+  // Verify that visitation uses INVOKE and supports a pointer-to-member.
+  struct X { int n; };
+  using V = std::variant<X, X*, std::reference_wrapper<X>>;
+  struct Derv : private V {
+    using V::V;
+    using V::visit;
+  };
+
+  Derv v{X{1}};
+  static_assert( std::is_same_v<decltype(v.visit(&X::n)), int&> );
+
+  // Verify that constness and value category are correctly forwarded.
+  std::variant<int> v2{1};
+  auto id = []<typename T>(T&& t) -> T&& { return std::forward<T>(t); };
+  static_assert( std::is_same_v<decltype(v2.visit(id)), int&> );
+  static_assert( std::is_same_v<decltype(std::move(v2).visit(id)), int&&> );
+  const auto& vc = v2;
+  static_assert( std::is_same_v<decltype(vc.visit(id)), const int&> );
+  static_assert( std::is_same_v<decltype(std::move(vc).visit(id)), const int&&> );
+
+  static_assert( std::is_same_v<decltype(vc.visit<void>(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<NoCopy, int> v{10};
+  NoCopy f;
+  // Visit should not need arguments to be copyable:
+  int res = v.visit(f);
+  VERIFY( res == 10 );
+  v.emplace<NoCopy>();
+  res = v.visit(f);
+  VERIFY( res == 100 );
+  res = v.visit<bool>(f);
+  VERIFY( res == 1 );
+}
+
+void
+test03()
+{
+  // Verify that member visit can access the variant as a private base class.
+  struct Derived : private std::variant<int>
+  {
+    using variant::visit;
+  };
+  Derived d;
+  int i = d.visit([](int& x) { return --x; });
+  VERIFY( i == -1 );
+  unsigned u = d.visit<unsigned>([](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<A, B, C>;
+  State state = A{};
+  auto res = std::move(state).visit<State>(f);
+  // Verify that visit<R> only matches the explicit return type overload.
+  static_assert( std::is_same_v<decltype(res), State> );
+}
+
+int
+main()
+{
+  test01();
+  test02();
+  test03();
+  test04();
+}