7
votes

The C++ core guidelines contain the following advice regarding the virtual, override and final specifiers, specifically relating to derived class destructors:

If a base class destructor is declared virtual, one should avoid declaring derived class destructors virtual or override. Some code base and tools might insist on override for destructors, but that is not the recommendation of these guidelines.

Sure enough, clang-tidy is one of those tools that goes against the recommendation. If I do not specify either virtual or override, running clang-tidy emits the following warning:

warning: annotate this function with 'override' or (rarely) 'final' [modernize-use-override]

or, if specified just as virtual:

warning: prefer using 'override' or (rarely) 'final' instead of 'virtual' [modernize-use-override]

My question

Given that specifying override, in particular, has the advantage of (at least) ensuing you have correctly specified the base class destructor as virtual, my question boils down to the following specific parts:

  1. What, if any, are the arguments in favour of not specifying virtual or override on derived class destructors?
  2. In your opinion, should I lean more towards the clang-tidy advice, or towards the C++ core guidelines advice?
1
This is the question that is on everyone's lips, in everyone's minds and (sometimes) in everyone's bedrooms.ben18785
This is an excellent question, but off-topic here on SO I think: this is opinion-based. You should join the discussion on github directly.YSC
@YSC 1. isn't opinion based.eerorika

1 Answers

-1
votes
  1. What, if any, are the arguments in favour of not specifying ... override on derived class destructors?

There are non-conformant compilers that fail to compile when destructor is specified with override: https://msdn.microsoft.com/en-us/library/xfzxhe2a.aspx.

  1. What, if any, are the arguments in favour of not specifying virtual ... on derived class destructors [when it is virtual implicitly]?

Less code is better. You save 7 characters worth by not using redundant virtual.

  • Counter argument: 7 characters is not much of a saving.

There might be a design decision to make a previously virtual destructor non-virtual for a base class and all of its children. If this guideline (of not specifying redundant virtual) is strictly followed, then only the base needs to be modified. Otherwise each child in the hierarchy must be modified (or at least checked whether they specify virtual separately).

  • Counter argument: This might be a completely hypothetical situation. Certainly quite rare.

  1. In your opinion, should I lean more towards the clang-tidy advice, or towards the C++ core guidelines advice?

The only argument that has significant weight in my opinion is the non-conformance of old Visual Studio compilers, and thus I would err on the side of not using override on destructors. Otherwise it matters very little and you can follow whichever guideline you prefer, or don't even bother following either - consistency isn't important when the subject at hand makes no difference.