aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2019-04-09 19:50:43 +0100
committerJonathan Wakely <redi@gcc.gnu.org>2019-04-09 19:50:43 +0100
commit8701cb5e0dc0a3b949bb28f3ff9ee5bae98c47fe (patch)
tree0bd9c3d6f3251d67f4acf2bfbdd6917989e32da5
parentbc203bf0dab0cdb39370357bc3244999a2f9e419 (diff)
downloadgcc-8701cb5e0dc0a3b949bb28f3ff9ee5bae98c47fe.zip
gcc-8701cb5e0dc0a3b949bb28f3ff9ee5bae98c47fe.tar.gz
gcc-8701cb5e0dc0a3b949bb28f3ff9ee5bae98c47fe.tar.bz2
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. From-SVN: r270237
-rw-r--r--libstdc++-v3/ChangeLog5
-rw-r--r--libstdc++-v3/include/std/variant24
-rw-r--r--libstdc++-v3/testsuite/20_util/variant/visit.cc73
3 files changed, 85 insertions, 17 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index 0d30d50..8fea9b6 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,5 +1,10 @@
2019-04-09 Jonathan Wakely <jwakely@redhat.com>
+ * 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.
+
PR libstdc++/90008
* include/std/variant (_VARIANT_RELATION_FUNCTION_TEMPLATE): Remove
unused capture.
diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant
index 603b6be..43e8d1d 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 0000000..5bd5b3d
--- /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();
+}