Hello,<br><br>I noticed that  -Wnon-virtual-destructor is not activated by default. In fact, it is not activated either by  -Wall  or  -Wextra.<br><br>This is a bit annoying, because it's an easy error to get into a scenario where one would needed, and the lack of it incurs Undefined Behavior.<br>
<br>However, the way the warning currently is implemented, it reports many false positives. The common case I could think of was:<br><br><div style="margin-left: 40px;">#include <iostream><br>#include <memory><br>
<br>class Base {<br>public:<br>  virtual int getId() const = 0;<br>protected:<br>  ~Base() {}<br>};<br><br>class Derived: public Base { // expect-warning {{'Derived' has virtual functions but non-virtual destructor [-Wnon-virtual-dtor]}}<br>
public:<br>  explicit Derived(int i): _id(i) {}<br>  virtual int getId() const { return _id; }<br>private:<br>  int _id;<br>};<br><br>void print(Base const& b) { std::cout << b.getId() << "\n"; }<br>
<br>int main(int argc, char* argv[]) {<br>  {<br>    Derived d(argc);<br>    print(d);<br>  }<br>  {<br>    std::auto_ptr<Derived> b(new Derived(argc));<br>    print(*b);<br>  }<br>  return 0;<br>}<br></div><br>Here, there is absolutely no need for a virtual destructor, yet a warning is emitted.<br>

<br>
This check is difficult. There are two main issues as I see it:<br>
- Policy Based design encourages you to inherit (privately, but still) 
from the policies in case they might be empty, to trigger the empty base
 optimization, thus you may inherit from non-polymorphic classes<br>
- It is impossible to know, at the point of `delete`, whether a class is used polymorphically or not (in another TU)<br><br>I was wondering if it would be possible to implement this check more conservatively, at the point of use: that is, on `delete`. Note that the array version does not need to be checked since arrays are not polymorphic.<br>
<br>More specifically, warning for a `delete` called on an object with virtual functions but no virtual destructors. Of course, this would still be imprecise. In the above case, the destruction of `std::auto_ptr<Derived>` is perfectly fine, yet would trigger the warning.<br>
<br>Still it would be less noisy, I think. It would notably nicely interact with Inversion of Control technics (such as Dependency Injection) where the object is built on the stack and passed by reference to an interface.<br>
<br>Also I believe the check would not be much more expensive that it is now (I don't think there would be much calls to `delete` on a given class in a single TU) and would silence many false positives.<br><br>Has it been attempted already ?<br>
<br>Is it doomed to fail ?<br><br>-- Matthieu<br>