aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2024-07-09 12:12:56 +0100
committerJonathan Wakely <redi@gcc.gnu.org>2024-07-31 17:07:10 +0100
commitf6f2c78d9db37228baa9d1f9926b0c120e24016f (patch)
treea1597efb8a49cb12f9df008511c8396e62d019a9
parent3836df7e895beda1f159620bfd20024136fda9f0 (diff)
downloadgcc-f6f2c78d9db37228baa9d1f9926b0c120e24016f.zip
gcc-f6f2c78d9db37228baa9d1f9926b0c120e24016f.tar.gz
gcc-f6f2c78d9db37228baa9d1f9926b0c120e24016f.tar.bz2
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.
-rw-r--r--libstdc++-v3/include/bits/version.def5
-rw-r--r--libstdc++-v3/include/bits/version.h7
-rw-r--r--libstdc++-v3/include/std/variant46
-rw-r--r--libstdc++-v3/testsuite/20_util/variant/visit.cc5
-rw-r--r--libstdc++-v3/testsuite/20_util/variant/visit_member.cc117
5 files changed, 178 insertions, 2 deletions
diff --git a/libstdc++-v3/include/bits/version.def b/libstdc++-v3/include/bits/version.def
index bcb33c1..806f1e9 100644
--- a/libstdc++-v3/include/bits/version.def
+++ b/libstdc++-v3/include/bits/version.def
@@ -479,6 +479,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;
extra_cond = "__cpp_concepts >= 202002L && __cpp_constexpr >= 201811L";
diff --git a/libstdc++-v3/include/bits/version.h b/libstdc++-v3/include/bits/version.h
index 4d1af34..e8ca0fa 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 3a23d9b..d0f7bd0 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,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<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&>;
+ 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&>;
+ 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 7f79e61..6edc7d7 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 0000000..90332b9
--- /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();
+}