Bug 96501 - [C++11] Should warn when classes only have copy constructor defined
Summary: [C++11] Should warn when classes only have copy constructor defined
Status: RESOLVED DUPLICATE of bug 89700
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 11.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
 
Reported: 2020-08-06 14:31 UTC by Nuno Lopes
Modified: 2021-05-19 16:13 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nuno Lopes 2020-08-06 14:31:15 UTC
I see a lot of old code that has copy constructors defined, but not move constructors. This pessimizes code since the definition of the copy constructor hides the default move constructor.
Would be nice to get a warning for this to recover perf.

```
#include <utility>
using namespace std;

struct foo {
  int a,b,c;
  // expect-warning
  foo(const foo &);
};

foo fn(foo &&f) {
  foo g(move(f));
  return g;
}
```

https://gcc.godbolt.org/z/5r8erG
Comment 1 Jonathan Wakely 2020-08-06 16:44:22 UTC
(In reply to Nuno Lopes from comment #0)
> I see a lot of old code that has copy constructors defined, but not move
> constructors. This pessimizes code since the definition of the copy
> constructor hides the default move constructor.

If you need a user-defined copy constructor, a defaulted move constructor probably isn't going to do the right thing anyway.
Comment 2 Jonathan Wakely 2020-08-06 17:29:57 UTC
Also, how would users suppress this warning for cases where it's not wanted?

Plenty of classes don't need a move constructor because moving is not more efficient than copying, but they don't want a defaulted move constructor.

Basically, the code isn't necessarily wrong, and there's no easy way to tell whether the lack of a move constructor is a problem, or should be changed.
Comment 3 Jonathan Wakely 2021-05-19 16:13:41 UTC
This is a dup, and there's additional discussion in the dup.

*** This bug has been marked as a duplicate of bug 89700 ***