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
This commit is contained in:
parent
bc203bf0da
commit
8701cb5e0d
@ -1,5 +1,10 @@
|
|||||||
2019-04-09 Jonathan Wakely <jwakely@redhat.com>
|
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
|
PR libstdc++/90008
|
||||||
* include/std/variant (_VARIANT_RELATION_FUNCTION_TEMPLATE): Remove
|
* include/std/variant (_VARIANT_RELATION_FUNCTION_TEMPLATE): Remove
|
||||||
unused capture.
|
unused capture.
|
||||||
|
@ -178,7 +178,7 @@ namespace __variant
|
|||||||
// used for raw visitation
|
// used for raw visitation
|
||||||
struct __variant_cookie {};
|
struct __variant_cookie {};
|
||||||
// used for raw visitation with indices passed in
|
// 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'
|
// a more explanatory name than 'true'
|
||||||
inline constexpr auto __visit_with_index = bool_constant<true>{};
|
inline constexpr auto __visit_with_index = bool_constant<true>{};
|
||||||
|
|
||||||
@ -1613,27 +1613,18 @@ namespace __variant
|
|||||||
return __detail::__variant::__get<_Np>(std::move(__v));
|
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,
|
template<bool __use_index,
|
||||||
bool __same_return_types,
|
bool __same_return_types,
|
||||||
typename _Visitor, typename... _Variants>
|
typename _Visitor, typename... _Variants>
|
||||||
constexpr decltype(auto)
|
constexpr decltype(auto)
|
||||||
__do_visit(_Visitor&& __visitor, _Variants&&... __variants)
|
__do_visit(_Visitor&& __visitor, _Variants&&... __variants)
|
||||||
{
|
{
|
||||||
using _Result_type =
|
using _Deduced_type = std::invoke_result<_Visitor,
|
||||||
decltype(__visitor_result_type<__use_index>(
|
decltype(std::get<0>(std::declval<_Variants>()))...>;
|
||||||
std::forward<_Visitor>(__visitor),
|
|
||||||
std::forward<_Variants>(__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<
|
constexpr auto& __vtable = __detail::__variant::__gen_vtable<
|
||||||
__same_return_types,
|
__same_return_types,
|
||||||
@ -1663,7 +1654,6 @@ namespace __variant
|
|||||||
if ((__variants.valueless_by_exception() || ...))
|
if ((__variants.valueless_by_exception() || ...))
|
||||||
__throw_bad_variant_access("Unexpected index");
|
__throw_bad_variant_access("Unexpected index");
|
||||||
|
|
||||||
|
|
||||||
if constexpr (std::is_void_v<_Res>)
|
if constexpr (std::is_void_v<_Res>)
|
||||||
(void) __do_visit<false, false>(std::forward<_Visitor>(__visitor),
|
(void) __do_visit<false, false>(std::forward<_Visitor>(__visitor),
|
||||||
std::forward<_Variants>(__variants)...);
|
std::forward<_Variants>(__variants)...);
|
||||||
|
73
libstdc++-v3/testsuite/20_util/variant/visit.cc
Normal file
73
libstdc++-v3/testsuite/20_util/variant/visit.cc
Normal file
@ -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();
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user