public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r12-9369] libstdc++: Use std::remove_cv_t in std::optional::transform [PR109242]
Date: Thu, 30 Mar 2023 08:08:35 +0000 (GMT)	[thread overview]
Message-ID: <20230330080835.6CFA13858CDB@sourceware.org> (raw)

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>>);

                 reply	other threads:[~2023-03-30  8:08 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230330080835.6CFA13858CDB@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).