[committed] libstdc++: Fix constraints on std::optional converting assignments [PR117858]

Jonathan Wakely jwakely@redhat.com
Sat Nov 30 21:39:44 GMT 2024


It looks like I copied these constraints from operator=(U&&) and didn't
correct them to account for the parameter being optional<U> not U.

libstdc++-v3/ChangeLog:

	PR libstdc++/117858
	* include/std/optional (operator=(const optional<U>&)): Fix copy
	and paste error in constraints.
	(operator=(optional<U>&&)): Likewise.
	* testsuite/20_util/optional/assignment/117858.cc: New test.
---

Tested x86_64-linux. Pushed to trunk.

 libstdc++-v3/include/std/optional                |  4 ++--
 .../20_util/optional/assignment/117858.cc        | 16 ++++++++++++++++
 2 files changed, 18 insertions(+), 2 deletions(-)
 create mode 100644 libstdc++-v3/testsuite/20_util/optional/assignment/117858.cc

diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional
index b8eedeec781..55e56cfb9ed 100644
--- a/libstdc++-v3/include/std/optional
+++ b/libstdc++-v3/include/std/optional
@@ -1043,7 +1043,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       template<typename _Up>
 #ifdef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL
-	requires (!is_same_v<optional, remove_cvref_t<_Up>>)
+	requires (!is_same_v<_Tp, _Up>)
 	  && is_constructible_v<_Tp, const _Up&>
 	  && is_assignable_v<_Tp&, const _Up&>
 	  && (!__converts_from_optional<_Tp, _Up>::value)
@@ -1077,7 +1077,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       template<typename _Up>
 #ifdef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL
-	requires (!is_same_v<optional, remove_cvref_t<_Up>>)
+	requires (!is_same_v<_Tp, _Up>)
 	  && is_constructible_v<_Tp, _Up>
 	  && is_assignable_v<_Tp&, _Up>
 	  && (!__converts_from_optional<_Tp, _Up>::value)
diff --git a/libstdc++-v3/testsuite/20_util/optional/assignment/117858.cc b/libstdc++-v3/testsuite/20_util/optional/assignment/117858.cc
new file mode 100644
index 00000000000..9443e160484
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/optional/assignment/117858.cc
@@ -0,0 +1,16 @@
+// { dg-do compile { target c++17 } }
+
+// PR 117858 std::optional with a constructor template<typename T> ctor(T)
+
+#include <optional>
+
+struct Focus
+{
+  template<class T>
+  Focus(T newValue)  { }
+};
+
+void g(std::optional<Focus> f)
+{
+  f = f;
+}
-- 
2.47.0



More information about the Libstdc++ mailing list