From: Jonathan Wakely Date: Wed, 29 Mar 2023 21:16:55 +0000 (+0100) Subject: libstdc++: Use std::remove_cv_t in std::optional::transform [PR109242] X-Git-Tag: releases/gcc-12.3.0~147 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=82e8d1685a1e5fac4880e987ed9684248378bce2;p=thirdparty%2Fgcc.git libstdc++: Use std::remove_cv_t in std::optional::transform [PR109242] We need to strip cv-qualifiers from the result of the callable passed to std::optional::transform. libstdc++-v3/ChangeLog: PR libstdc++/109242 * include/std/optional (transform): Use std::remove_cv_t. * testsuite/20_util/optional/monadic/pr109242.cc: New test. (cherry picked from commit 31a909712014b75fc6ae2ca5eaa425f218bb5f32) --- diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional index 791ef6f1994b..122ff7e58630 100644 --- a/libstdc++-v3/include/std/optional +++ b/libstdc++-v3/include/std/optional @@ -1100,7 +1100,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr auto transform(_Fn&& __f) & { - using _Up = invoke_result_t<_Fn, _Tp&>; + using _Up = remove_cv_t>; if (has_value()) return optional<_Up>(_Optional_func<_Fn>{__f}, **this); else @@ -1111,7 +1111,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr auto transform(_Fn&& __f) const & { - using _Up = invoke_result_t<_Fn, const _Tp&>; + using _Up = remove_cv_t>; if (has_value()) return optional<_Up>(_Optional_func<_Fn>{__f}, **this); else @@ -1122,7 +1122,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr auto transform(_Fn&& __f) && { - using _Up = invoke_result_t<_Fn, _Tp>; + using _Up = remove_cv_t>; if (has_value()) return optional<_Up>(_Optional_func<_Fn>{__f}, std::move(**this)); else @@ -1133,7 +1133,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr auto transform(_Fn&& __f) const && { - using _Up = invoke_result_t<_Fn, const _Tp>; + using _Up = remove_cv_t>; if (has_value()) return optional<_Up>(_Optional_func<_Fn>{__f}, std::move(**this)); else diff --git a/libstdc++-v3/testsuite/20_util/optional/monadic/pr109242.cc b/libstdc++-v3/testsuite/20_util/optional/monadic/pr109242.cc new file mode 100644 index 000000000000..a25b6251589a --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/monadic/pr109242.cc @@ -0,0 +1,35 @@ +// { dg-options "-std=gnu++23" } +// { dg-do compile { target c++23 } } + +#include + +// PR libstdc++/109242 +// transform omits required std::remove_cv_t from return optional type + +struct A { }; +struct B { }; +struct C { }; +struct D { }; + +struct F +{ + const A operator()(int&); + const B operator()(const int&); + const C operator()(int&&); + const D operator()(const int&&); +} f; + +std::optional o; +const auto& co = o; + +auto o1 = o.transform(f); +static_assert(std::is_same_v>); + +auto o2 = co.transform(f); +static_assert(std::is_same_v>); + +auto o3 = std::move(o).transform(f); +static_assert(std::is_same_v>); + +auto o4 = std::move(co).transform(f); +static_assert(std::is_same_v>);