override for virtual destructors in c++11

1,477 views
Skip to first unread message

Michał Kowalczyk

unread,
Mar 24, 2017, 5:32:47 AM3/24/17
to std-dis...@isocpp.org
Hi,
In our team, we have a discussion about using override with virtual destructors.
We found no information in the standard that it is even allowed but most compilers support it.

We see that Google in its coding guidelines encourages using override with destructors:
"Explicitly annotate overrides of virtual functions or virtual destructors with an override or (less frequently) final specifier."

On the other hand C++ Core Guidelines says:
"If a base class destructor is declared virtual, derived class destructors should neither be declared virtual nor override."

Did anyone find any superior answer for using override with destructors?

Thanks,
Michał Kowalczyk
www.mkk.ekk.pl

Ville Voutilainen

unread,
Mar 24, 2017, 5:47:14 AM3/24/17
to std-dis...@isocpp.org
On 24 March 2017 at 11:32, Michał Kowalczyk <m...@ekk.pl> wrote:
> Hi,
> In our team, we have a discussion about using override with virtual
> destructors.
> We found no information in the standard that it is even allowed but most
> compilers support it.

The standard does allow it, since virt-specifiers can be used with a
virtual function declaration,
and there's no special restriction that would prevent using them with
a destructor.

> We see that Google in its coding guidelines encourages using override with
> destructors:
> "Explicitly annotate overrides of virtual functions or virtual destructors
> with an override or (less frequently) final specifier."
> source: https://google.github.io/styleguide/cppguide.html
>
> On the other hand C++ Core Guidelines says:
> "If a base class destructor is declared virtual, derived class destructors
> should neither be declared virtual nor override."
> src:
> https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md
>
> Did anyone find any superior answer for using override with destructors?

The benefit of using override with a destructor is that it also
ensures that the destructor is virtual.
Some people believe that that is unnecessary boiler-plate, and yet
others believe that if the base
class destructor is changed from virtual to non-virtual, it's harmful
to need to adjust derived
class destructors.

In other words, there is no superior answer that works for every use case.

Michał Kowalczyk

unread,
Mar 24, 2017, 6:48:10 AM3/24/17
to std-dis...@isocpp.org
It seems we misinterpreted the standard. Thanks Ville for your prompt response.

M.



--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+unsubscribe@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.

Tony V E

unread,
Mar 25, 2017, 10:40:01 PM3/25/17
to std-discussion
If the base class destructor is changed to non-virtual, I think it would be great if there was a compiler error somewhere, as there is probably some code deleting via the base class, and that code is now leaking.

But hopefully that isn't the type of change done lightly, without reviewing the consequences.


--
Be seeing you,
Tony

Ville Voutilainen

unread,
Mar 25, 2017, 10:49:45 PM3/25/17
to std-dis...@isocpp.org
On 26 March 2017 at 05:39, Tony V E <tvan...@gmail.com> wrote:
>> The benefit of using override with a destructor is that it also
>> ensures that the destructor is virtual.
>> Some people believe that that is unnecessary boiler-plate, and yet
>> others believe that if the base
>> class destructor is changed from virtual to non-virtual, it's harmful
>> to need to adjust derived
>> class destructors.
>>
>
> If the base class destructor is changed to non-virtual, I think it would be
> great if there was a compiler error somewhere, as there is probably some
> code deleting via the base class, and that code is now leaking.

There's a non-zero chance that such a change also changes access from public to
protected.

Thiago Macieira

unread,
Mar 26, 2017, 1:53:54 AM3/26/17
to std-dis...@isocpp.org
On sábado, 25 de março de 2017 19:49:43 PDT Ville Voutilainen wrote:
> There's a non-zero chance that such a change also changes access from public
> to protected.

Unless it's a major refactoring that changes a previously-polymorphic class to
be value-type.

--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center

Reply all
Reply to author
Forward
0 new messages