From: Jonathan Wakely Date: Tue, 9 Apr 2019 18:50:43 +0000 (+0100) Subject: Fix std::visit to support arbitrary callables X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=8701cb5e0dc0a3b949bb28f3ff9ee5bae98c47fe;p=gcc.git 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 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 0d30d50b17c..8fea9b6ccab 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,10 @@ 2019-04-09 Jonathan Wakely + * 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 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{}; @@ -1613,27 +1613,18 @@ namespace __variant return __detail::__variant::__get<_Np>(std::move(__v)); } - template - 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 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(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 +// . + +// { dg-options "-std=gnu++17" } +// { dg-do run { target c++17 } } + +#include +#include +#include + +// 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> vobj{X{1}}; + int res = std::visit(&X::n, vobj); + VERIFY( res == 1 ); + + std::variant 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(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(); +}