This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [PATCH 2/3] Fix std::visit to support arbitrary callables
- From: Jonathan Wakely <jwakely at redhat dot com>
- To: libstdc++ at gcc dot gnu dot org, gcc-patches at gcc dot gnu dot org
- Date: Tue, 9 Apr 2019 19:52:03 +0100
- Subject: Re: [PATCH 2/3] Fix std::visit to support arbitrary callables
- References: <20190409185038.GA16127@redhat.com>
The __visitor_result_type helper didn't use std::invoke and so didn't
compile when the visitor was a pointer-to-member rather than a function
object. Use std::invoke_result instead.
* include/std/variant (__variant_idx_cookie): Add member type.
(__visitor_result_type): Remove.
(__do_visit): Use invoke_result instead of __visitor_result_type.
* testsuite/20_util/variant/visit.cc: New test.
Tested powerpc64le-linux, committed to trunk.
commit d0166594867b89afa636b65f4dcd583f06f55220
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Tue Apr 9 11:16:23 2019 +0100
Fix std::visit to support arbitrary callables
The __visitor_result_type helper didn't use std::invoke and so didn't
compile when the visitor was a pointer-to-member rather than a function
object. Use std::invoke_result instead.
* include/std/variant (__variant_idx_cookie): Add member type.
(__visitor_result_type): Remove.
(__do_visit): Use invoke_result instead of __visitor_result_type.
* testsuite/20_util/variant/visit.cc: New test.
diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant
index 603b6be0934..43e8d1d1706 100644
--- a/libstdc++-v3/include/std/variant
+++ b/libstdc++-v3/include/std/variant
@@ -178,7 +178,7 @@ namespace __variant
// used for raw visitation
struct __variant_cookie {};
// used for raw visitation with indices passed in
- struct __variant_idx_cookie {};
+ struct __variant_idx_cookie { using type = __variant_idx_cookie; };
// a more explanatory name than 'true'
inline constexpr auto __visit_with_index = bool_constant<true>{};
@@ -1613,27 +1613,18 @@ namespace __variant
return __detail::__variant::__get<_Np>(std::move(__v));
}
- template<bool __use_index, typename _Visitor, typename... _Variants>
- decltype(auto)
- __visitor_result_type(_Visitor&& __visitor, _Variants&&... __variants)
- {
- if constexpr(__use_index)
- return __detail::__variant::__variant_idx_cookie{};
- else
- return std::forward<_Visitor>(__visitor)(
- std::get<0>(std::forward<_Variants>(__variants))...);
- }
-
template<bool __use_index,
bool __same_return_types,
typename _Visitor, typename... _Variants>
constexpr decltype(auto)
__do_visit(_Visitor&& __visitor, _Variants&&... __variants)
{
- using _Result_type =
- decltype(__visitor_result_type<__use_index>(
- std::forward<_Visitor>(__visitor),
- std::forward<_Variants>(__variants)...));
+ using _Deduced_type = std::invoke_result<_Visitor,
+ decltype(std::get<0>(std::declval<_Variants>()))...>;
+
+ using _Result_type = typename std::conditional_t<__use_index,
+ __detail::__variant::__variant_idx_cookie,
+ _Deduced_type>::type;
constexpr auto& __vtable = __detail::__variant::__gen_vtable<
__same_return_types,
@@ -1663,7 +1654,6 @@ namespace __variant
if ((__variants.valueless_by_exception() || ...))
__throw_bad_variant_access("Unexpected index");
-
if constexpr (std::is_void_v<_Res>)
(void) __do_visit<false, false>(std::forward<_Visitor>(__visitor),
std::forward<_Variants>(__variants)...);
diff --git a/libstdc++-v3/testsuite/20_util/variant/visit.cc b/libstdc++-v3/testsuite/20_util/variant/visit.cc
new file mode 100644
index 00000000000..5bd5b3d11f7
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/variant/visit.cc
@@ -0,0 +1,73 @@
+// Copyright (C) 2019 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-options "-std=gnu++17" }
+// { dg-do run { target c++17 } }
+
+#include <variant>
+#include <functional>
+#include <testsuite_hooks.h>
+
+// N.B. there are more std::visit tests in ./compile.cc and ./run.cc
+
+void
+test01()
+{
+ // Verify that visitation uses INVOKE and supports arbitrary callables.
+
+ struct X
+ {
+ int sum(int i) const { return i + n; }
+ int product(int i) const { return i * n; }
+ int n;
+ };
+
+ std::variant<X, X*, std::reference_wrapper<X>> vobj{X{1}};
+ int res = std::visit(&X::n, vobj);
+ VERIFY( res == 1 );
+
+ std::variant<int, short> varg{2};
+ res = std::visit(&X::sum, vobj, varg);
+ VERIFY( res == 3 );
+
+ X x{4};
+ vobj = &x;
+ res = std::visit(&X::n, vobj);
+ VERIFY( res == 4 );
+
+ varg.emplace<short>(5);
+ res = std::visit(&X::sum, vobj, varg);
+ VERIFY( res == 9 );
+
+ x.n = 6;
+ res = std::visit(&X::product, vobj, varg);
+ VERIFY( res == 30 );
+
+ vobj = std::ref(x);
+ x.n = 7;
+ res = std::visit(&X::n, vobj);
+ VERIFY( res == 7 );
+
+ res = std::visit(&X::product, vobj, varg);
+ VERIFY( res == 35 );
+}
+
+int
+main()
+{
+ test01();
+}