May a C++ destructor be declared as
final
?
Yes.
And if so, does that prevent declaration of a derived class:
Yes, because the derived class would have to declare a destructor (either explicitly by you or implicitly by the compiler), and that destructor would be overriding a function declared final
, which is ill-formed.
The rule is [class.virtual]/4:
If a virtual function
f
in some class B is marked with the virt-specifierfinal
and in a class D derived from B a functionD::f
overridesB::f
, the program is ill-formed.
It’s the derivation itself that is ill-formed, it doesn’t have to be used.
Is declaring a destructor to be final a workable idiom for indicating that a class is not intended to be used as a base class?
Effectively, but you should just mark the class final
. It’s quite a bit more explicit.