http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53596

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-06-07 
08:46:09 UTC ---
(In reply to comment #3)
> Hi, thanks for your answer but I still think it is a bug. Here are my reasons:
> 
> 1) You mention that Clang also warns for my example but this seems to be a bug
> in Clang, see
> http://lists.cs.uiuc.edu/pipermail/llvmbugs/2012-March/022451.html

I expect that bug will get closed, Clang behaves as designed.

> 2) Herb Sutter wrote a Guru of the Week (GotW) article on this topic
> (http://www.gotw.ca/publications/mill18.htm), it says: 
> 
> Guideline #4: A base class destructor should be either public and virtual, or
> protected and nonvirtual.

The compiler cannot know Derived is not a base class (unless you mark it
"final" to prevent derivation, or "__final" as a GCC extension in C++03).  You
have virtual functions, so it's reasonable to assume you plan to derive from
it.

> 3) If I modify 'virtual void foo()' to 'void foo()' in my previous example the
> warning vanishes, this makes no sense.

Then the class isn't polymorphic, so the compiler assumes you don't plan to
derive from it.


> 4) In your MoreDerived example you can make the Derived dtor virtual, this way
> the protected non-virtual Base destructor is absolutely valid.

Obviously. That fixes the undefined behaviour, so there's no warning,  that's
the change the compiler is suggesting you make!

(In reply to comment #4)
> I found another post on the web
> (http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20110530/042333.html)
> that explains the warning, it says:
> 
> "If Derived (see above) gets subclassed in the future, deletion in "main()"
> will be undefined behavior. Ideally Derived should be marked "final" and then
> there will also be no warning"

That's exactly what I demonstrated in my MoreDerived example.

> Based on this explanation I think the warning is valid. Clang's warning 
> message
> is somewhat more clear:
> 
> "warning: delete called on 'Derived' that has virtual functions but 
> non-virtual
> destructor [-Wdelete-non-virtual-dtor]"

A polymorphic class is one with virtual functions. That's standard terminology.

I prefer the GCC message, it tells you using delete might (or will) be
undefined behaviour, instead of just warning about using delete.

> This also explains why there is no warning in test2.cpp of my previous post.

Exactly.

Reply via email to