libstdc++: Fix constraint on std::optional assignment [PR 100982]
authorJonathan Wakely <jwakely@redhat.com>
Wed, 9 Jun 2021 10:03:15 +0000 (11:03 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 9 Jun 2021 11:45:11 +0000 (12:45 +0100)
libstdc++-v3/ChangeLog:

PR libstdc++/100982
* include/std/optional (optional::operator=(const optional<U>&)):
Fix value category used in is_assignable check.
* testsuite/20_util/optional/assignment/100982.cc: New test.

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

index 415f8c4..0a67ce2 100644 (file)
@@ -815,7 +815,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       template<typename _Up>
        enable_if_t<__and_v<__not_<is_same<_Tp, _Up>>,
                            is_constructible<_Tp, const _Up&>,
-                           is_assignable<_Tp&, _Up>,
+                           is_assignable<_Tp&, const _Up&>,
                            __not_<__converts_from_optional<_Tp, _Up>>,
                            __not_<__assigns_from_optional<_Tp, _Up>>>,
                    optional&>
diff --git a/libstdc++-v3/testsuite/20_util/optional/assignment/100982.cc b/libstdc++-v3/testsuite/20_util/optional/assignment/100982.cc
new file mode 100644 (file)
index 0000000..ae56525
--- /dev/null
@@ -0,0 +1,17 @@
+// { dg-do compile { target c++17 } }
+
+#include <optional>
+
+struct U {};
+
+struct T {
+  explicit T(const U&);
+  T& operator=(const U&);
+  T& operator=(U&&) = delete;
+};
+
+int main() {
+  std::optional<U> opt1;
+  std::optional<T> opt2;
+  opt2 = opt1; // PR libstdc++/100982
+}