]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Use std::remove_cv_t in std::optional::transform [PR109242]
authorJonathan Wakely <jwakely@redhat.com>
Wed, 29 Mar 2023 21:16:55 +0000 (22:16 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 30 Mar 2023 08:07:53 +0000 (09:07 +0100)
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)

libstdc++-v3/include/std/optional
libstdc++-v3/testsuite/20_util/optional/monadic/pr109242.cc [new file with mode: 0644]

index 791ef6f1994b25e72d65b21a7175bf9f2dae1e18..122ff7e5863057e02912ded9f4d418436fd65685 100644 (file)
@@ -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 (file)
index 0000000..a25b625
--- /dev/null
@@ -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>>);