]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix constraints on std::optional converting assignments [PR117858]
authorJonathan Wakely <jwakely@redhat.com>
Sat, 30 Nov 2024 21:37:02 +0000 (21:37 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Sat, 30 Nov 2024 21:39:17 +0000 (21:39 +0000)
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.

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

index b8eedeec7817fb5681ec1236f40528f08d01884b..55e56cfb9ed1b54d0ece3de3b7f35aecfa5649fc 100644 (file)
@@ -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 (file)
index 0000000..9443e16
--- /dev/null
@@ -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;
+}