This bug report mentions this test case which doesn't emit a warning in gcc:
struct W {
int a;
~W() { a = 9; }
};
int main() {
W w {};
W v = w;
}
Refer to Johnathan Wakely's comment:
That's not true, the compiler can (and does) warn about legal code.
I'm confirming this, we will want the warning at some point, and it
would allow us to improve this part of the -Weffc++
warnings:
* Item 11: Define a copy constructor and an assignment operator for classes with dynamically allocated memory.
(see PR 16166
for more details)
Maybe we could call this warning -Wdeprecated-special-members
, and
have it enabled -Weffc++
and in C++11 also by -Wdeprecated
Clang already warns about this with -Wdeprecated
:
main.cpp:3:3: warning: definition of implicit copy constructor for 'W' is deprecated because it has a user-declared destructor [-Wdeprecated]
~W() { a = 9; }
^
main.cpp:8:8: note: implicit copy constructor for 'W' first required here
W v = w;
Microsoft explicitly states that Visual Studio will not emit a warning in this case:
Additionally, the C++11 standard specifies the following additional
rules:
If a copy constructor or destructor is explicitly declared, then automatic generation of the copy-assignment operator is deprecated.
If a copy-assignment operator or destructor is explicitly declared, then automatic generation of the copy constructor is deprecated.
In both cases, Visual Studio continues to automatically generate the
necessary functions implicitly, and does not emit a warning.