libstdc++: Use emplace in std::variant::operator=(T&&) as per LWG 3585
authorJonathan Wakely <jwakely@redhat.com>
Wed, 1 Feb 2023 20:54:22 +0000 (20:54 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 2 Feb 2023 16:18:37 +0000 (16:18 +0000)
This was approved at the October 2021 plenary.

libstdc++-v3/ChangeLog:

* include/std/variant (variant::operator=): Implement resolution
of LWG 3585.
* testsuite/20_util/variant/lwg3585.cc: New test.

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

index 3578149..5155124 100644 (file)
@@ -1481,7 +1481,9 @@ namespace __variant
                            || !is_nothrow_move_constructible_v<_Tj>)
                this->emplace<__index>(std::forward<_Tp>(__rhs));
              else
-               operator=(variant(std::forward<_Tp>(__rhs)));
+               // _GLIBCXX_RESOLVE_LIB_DEFECTS
+               // 3585. converting assignment with immovable alternative
+               this->emplace<__index>(_Tj(std::forward<_Tp>(__rhs)));
            }
          return *this;
        }
diff --git a/libstdc++-v3/testsuite/20_util/variant/lwg3585.cc b/libstdc++-v3/testsuite/20_util/variant/lwg3585.cc
new file mode 100644 (file)
index 0000000..0cbfc0d
--- /dev/null
@@ -0,0 +1,16 @@
+// { dg-do compile { target c++17 } }
+
+// LWG 3585. Variant converting assignment with immovable alternative
+
+#include <variant>
+#include <string>
+
+struct A {
+  A() = default;
+  A(A&&) = delete;
+};
+
+int main() {
+  std::variant<A, std::string> v;
+  v = "hello";
+}