public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-9369] libstdc++: Use std::remove_cv_t in std::optional::transform [PR109242]
@ 2023-03-30  8:08 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2023-03-30  8:08 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:82e8d1685a1e5fac4880e987ed9684248378bce2

commit r12-9369-g82e8d1685a1e5fac4880e987ed9684248378bce2
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Mar 29 22:16:55 2023 +0100

    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:
---
 libstdc++-v3/include/std/optional                  |  8 ++---
 .../testsuite/20_util/optional/monadic/pr109242.cc | 35 ++++++++++++++++++++++
 2 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional
index 791ef6f1994..122ff7e5863 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<invoke_result_t<_Fn, _Tp&>>;
 	  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<invoke_result_t<_Fn, const _Tp&>>;
 	  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<invoke_result_t<_Fn, _Tp>>;
 	  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<invoke_result_t<_Fn, const _Tp>>;
 	  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 00000000000..a25b6251589
--- /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 <optional>
+
+// 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<int> o;
+const auto& co = o;
+
+auto o1 = o.transform(f);
+static_assert(std::is_same_v<decltype(o1), std::optional<A>>);
+
+auto o2 = co.transform(f);
+static_assert(std::is_same_v<decltype(o2), std::optional<B>>);
+
+auto o3 = std::move(o).transform(f);
+static_assert(std::is_same_v<decltype(o3), std::optional<C>>);
+
+auto o4 = std::move(co).transform(f);
+static_assert(std::is_same_v<decltype(o4), std::optional<D>>);

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-03-30  8:08 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-30  8:08 [gcc r12-9369] libstdc++: Use std::remove_cv_t in std::optional::transform [PR109242] Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).