]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: -Wdeprecated-copy and using operator= [PR92145]
authorJason Merrill <jason@redhat.com>
Fri, 23 Apr 2021 20:41:35 +0000 (16:41 -0400)
committerJason Merrill <jason@redhat.com>
Wed, 20 Dec 2023 17:41:03 +0000 (12:41 -0500)
For the purpose of [depr.impldec] "if the class has a user-declared copy
assignment operator", an operator= brought in from a base class with 'using'
may be a copy-assignment operator, but it isn't a copy-assignment operator
for the derived class.

gcc/cp/ChangeLog:

PR c++/92145
* class.c (classtype_has_depr_implicit_copy): Check DECL_CONTEXT
of operator=.

gcc/testsuite/ChangeLog:

PR c++/92145
* g++.dg/cpp0x/depr-copy3.C: New test.

(cherry picked from commit 37846c42f1f5ac4d9ba190d49c4373673c89c8b5)

gcc/cp/class.c
gcc/testsuite/g++.dg/cpp0x/depr-copy3.C [new file with mode: 0644]

index 3251aaa1a46cf3a8cf6d7001e32fae8ffc1349d1..32667255a857fc208887726fa711e47210e336c8 100644 (file)
@@ -5685,7 +5685,8 @@ classtype_has_depr_implicit_copy (tree t)
         iter; ++iter)
       {
        tree fn = *iter;
-       if (user_provided_p (fn) && copy_fn_p (fn))
+       if (DECL_CONTEXT (fn) == t
+           && user_provided_p (fn) && copy_fn_p (fn))
          return fn;
       }
 
diff --git a/gcc/testsuite/g++.dg/cpp0x/depr-copy3.C b/gcc/testsuite/g++.dg/cpp0x/depr-copy3.C
new file mode 100644 (file)
index 0000000..c303c9d
--- /dev/null
@@ -0,0 +1,35 @@
+// PR c++/92145
+// { dg-do compile { target c++11 } }
+// { dg-additional-options "-Wdeprecated-copy" }
+
+struct base
+{
+  base() { }
+  base(const base&) { }
+  base(base&&) { }
+  base& operator=(const base&) { return *this; }
+  base& operator=(base&&) { return *this; }
+};
+
+struct foo : base
+{
+  //using base::base;
+  using base::operator=;
+};
+
+struct bar
+{
+  bar& operator=(foo v)
+  {
+    value = v;
+    return *this;
+  }
+
+  foo value;
+};
+
+int main()
+{
+  foo a;
+  foo{a};
+}