public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [committed] libstdc++: Use std::remove_cv_t in std::optional::transform [PR109340]
@ 2023-03-29 23:39 Jonathan Wakely
  2023-03-29 23:45 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Jonathan Wakely @ 2023-03-29 23:39 UTC (permalink / raw)
  To: libstdc++, gcc-patches

Tested powerpc64le-linux. Pushed to trunk.

-- >8 --

We need to strip cv-qualifiers from the result of the callable passed to
std::optional::transform.

For std::expected::transform and std::expected::transform_error I
noticed we were stripping cv-qualifiers but were also incorrectly
stripping references.

libstdc++-v3/ChangeLog:

	PR libstdc++/109340
	* include/std/expected (expected::transform): Use
	std::remove_cv_t instead of std::remove_cvref_t.
	(expected::transform_error): Likewise.
	(expected<cv void, E>::transform): Likewise.
	(expected<cv void, E>::transform_error): Likewise.
	* include/std/optional (transform): Use std::remove_cv_t.
	* testsuite/20_util/optional/monadic/pr109340.cc: New test.
---
 libstdc++-v3/include/std/expected             | 36 ++++++++++---------
 libstdc++-v3/include/std/optional             |  8 ++---
 .../20_util/optional/monadic/pr109340.cc      | 35 ++++++++++++++++++
 3 files changed, 59 insertions(+), 20 deletions(-)
 create mode 100644 libstdc++-v3/testsuite/20_util/optional/monadic/pr109340.cc

diff --git a/libstdc++-v3/include/std/expected b/libstdc++-v3/include/std/expected
index 567a5195e8d..cb5754e2a68 100644
--- a/libstdc++-v3/include/std/expected
+++ b/libstdc++-v3/include/std/expected
@@ -154,8 +154,12 @@ namespace __expected
 
   template<typename _Fn, typename _Tp>
     using __result = remove_cvref_t<invoke_result_t<_Fn&&, _Tp&&>>;
+  template<typename _Fn, typename _Tp>
+    using __result_xform = remove_cv_t<invoke_result_t<_Fn&&, _Tp&&>>;
   template<typename _Fn>
     using __result0 = remove_cvref_t<invoke_result_t<_Fn&&>>;
+  template<typename _Fn>
+    using __result0_xform = remove_cv_t<invoke_result_t<_Fn&&>>;
 
   template<typename _Er>
     concept __can_be_unexpected
@@ -953,7 +957,7 @@ namespace __expected
 	constexpr auto
 	transform(_Fn&& __f) &
 	{
-	  using _Up = __expected::__result<_Fn, _Tp&>;
+	  using _Up = __expected::__result_xform<_Fn, _Tp&>;
 	  using _Res = expected<_Up, _Er>;
 
 	  if (has_value())
@@ -969,7 +973,7 @@ namespace __expected
 	constexpr auto
 	transform(_Fn&& __f) const &
 	{
-	  using _Up = __expected::__result<_Fn, const _Tp&>;
+	  using _Up = __expected::__result_xform<_Fn, const _Tp&>;
 	  using _Res = expected<_Up, _Er>;
 
 	  if (has_value())
@@ -985,7 +989,7 @@ namespace __expected
 	constexpr auto
 	transform(_Fn&& __f) &&
 	{
-	  using _Up = __expected::__result<_Fn, _Tp>;
+	  using _Up = __expected::__result_xform<_Fn, _Tp>;
 	  using _Res = expected<_Up, _Er>;
 
 	  if (has_value())
@@ -1001,7 +1005,7 @@ namespace __expected
 	constexpr auto
 	transform(_Fn&& __f) const &&
 	{
-	  using _Up = __expected::__result<_Fn, const _Tp>;
+	  using _Up = __expected::__result_xform<_Fn, const _Tp>;
 	  using _Res = expected<_Up, _Er>;
 
 	  if (has_value())
@@ -1017,7 +1021,7 @@ namespace __expected
 	constexpr auto
 	transform_error(_Fn&& __f) &
 	{
-	  using _Gr = __expected::__result<_Fn, _Er&>;
+	  using _Gr = __expected::__result_xform<_Fn, _Er&>;
 	  using _Res = expected<_Tp, _Gr>;
 
 	  if (has_value())
@@ -1033,7 +1037,7 @@ namespace __expected
 	constexpr auto
 	transform_error(_Fn&& __f) const &
 	{
-	  using _Gr = __expected::__result<_Fn, const _Er&>;
+	  using _Gr = __expected::__result_xform<_Fn, const _Er&>;
 	  using _Res = expected<_Tp, _Gr>;
 
 	  if (has_value())
@@ -1049,7 +1053,7 @@ namespace __expected
 	constexpr auto
 	transform_error(_Fn&& __f) &&
 	{
-	  using _Gr = __expected::__result<_Fn, _Er&&>;
+	  using _Gr = __expected::__result_xform<_Fn, _Er&&>;
 	  using _Res = expected<_Tp, _Gr>;
 
 	  if (has_value())
@@ -1065,7 +1069,7 @@ namespace __expected
 	constexpr auto
 	transform_error(_Fn&& __f) const &&
 	{
-	  using _Gr = __expected::__result<_Fn, const _Er&&>;
+	  using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
 	  using _Res = expected<_Tp, _Gr>;
 
 	  if (has_value())
@@ -1630,7 +1634,7 @@ namespace __expected
 	constexpr auto
 	transform(_Fn&& __f) &
 	{
-	  using _Up = __expected::__result0<_Fn>;
+	  using _Up = __expected::__result0_xform<_Fn>;
 	  using _Res = expected<_Up, _Er>;
 
 	  if (has_value())
@@ -1643,7 +1647,7 @@ namespace __expected
 	constexpr auto
 	transform(_Fn&& __f) const &
 	{
-	  using _Up = __expected::__result0<_Fn>;
+	  using _Up = __expected::__result0_xform<_Fn>;
 	  using _Res = expected<_Up, _Er>;
 
 	  if (has_value())
@@ -1656,7 +1660,7 @@ namespace __expected
 	constexpr auto
 	transform(_Fn&& __f) &&
 	{
-	  using _Up = __expected::__result0<_Fn>;
+	  using _Up = __expected::__result0_xform<_Fn>;
 	  using _Res = expected<_Up, _Er>;
 
 	  if (has_value())
@@ -1669,7 +1673,7 @@ namespace __expected
 	constexpr auto
 	transform(_Fn&& __f) const &&
 	{
-	  using _Up = __expected::__result0<_Fn>;
+	  using _Up = __expected::__result0_xform<_Fn>;
 	  using _Res = expected<_Up, _Er>;
 
 	  if (has_value())
@@ -1682,7 +1686,7 @@ namespace __expected
 	constexpr auto
 	transform_error(_Fn&& __f) &
 	{
-	  using _Gr = __expected::__result<_Fn, _Er&>;
+	  using _Gr = __expected::__result_xform<_Fn, _Er&>;
 	  using _Res = expected<_Tp, _Gr>;
 
 	  if (has_value())
@@ -1698,7 +1702,7 @@ namespace __expected
 	constexpr auto
 	transform_error(_Fn&& __f) const &
 	{
-	  using _Gr = __expected::__result<_Fn, const _Er&>;
+	  using _Gr = __expected::__result_xform<_Fn, const _Er&>;
 	  using _Res = expected<_Tp, _Gr>;
 
 	  if (has_value())
@@ -1714,7 +1718,7 @@ namespace __expected
 	constexpr auto
 	transform_error(_Fn&& __f) &&
 	{
-	  using _Gr = __expected::__result<_Fn, _Er&&>;
+	  using _Gr = __expected::__result_xform<_Fn, _Er&&>;
 	  using _Res = expected<_Tp, _Gr>;
 
 	  if (has_value())
@@ -1730,7 +1734,7 @@ namespace __expected
 	constexpr auto
 	transform_error(_Fn&& __f) const &&
 	{
-	  using _Gr = __expected::__result<_Fn, const _Er&&>;
+	  using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
 	  using _Res = expected<_Tp, _Gr>;
 
 	  if (has_value())
diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional
index 90bf74143f4..bcac1522701 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/pr109340.cc b/libstdc++-v3/testsuite/20_util/optional/monadic/pr109340.cc
new file mode 100644
index 00000000000..a25b6251589
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/optional/monadic/pr109340.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>>);
-- 
2.39.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [committed] libstdc++: Use std::remove_cv_t in std::optional::transform [PR109340]
  2023-03-29 23:39 [committed] libstdc++: Use std::remove_cv_t in std::optional::transform [PR109340] Jonathan Wakely
@ 2023-03-29 23:45 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2023-03-29 23:45 UTC (permalink / raw)
  To: libstdc++, gcc-patches

On 30/03/23 00:39 +0100, Jonathan Wakely wrote:
>Tested powerpc64le-linux. Pushed to trunk.
>
>-- >8 --
>
>We need to strip cv-qualifiers from the result of the callable passed to
>std::optional::transform.
>
>For std::expected::transform and std::expected::transform_error I
>noticed we were stripping cv-qualifiers but were also incorrectly
>stripping references.
>
>libstdc++-v3/ChangeLog:
>
>	PR libstdc++/109340
>	* include/std/expected (expected::transform): Use
>	std::remove_cv_t instead of std::remove_cvref_t.
>	(expected::transform_error): Likewise.
>	(expected<cv void, E>::transform): Likewise.
>	(expected<cv void, E>::transform_error): Likewise.
>	* include/std/optional (transform): Use std::remove_cv_t.
>	* testsuite/20_util/optional/monadic/pr109340.cc: New test.

Oops, I put the wrong PR number in the commit message and the name of
the new test. I've renamed it to pr109242.cc now.



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-03-29 23:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-29 23:39 [committed] libstdc++: Use std::remove_cv_t in std::optional::transform [PR109340] Jonathan Wakely
2023-03-29 23:45 ` 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).