Historically we've had the NS_FINAL_CLASS macro to annotate classes which should never be inherited. This relied on static analysis beyond normal builds for correctness. C++11 adds a contextual "final" keyword for this, which is now encapsulated in the MOZ_FINAL macro:
#include "mozilla/Attributes.h"
class F MOZ_FINAL { };
class D : public F { }; // ERROR: F is final
class B { };
class D2 MOZ_FINAL : public B { };
The MOZ_FINAL macro supplants the old, gunky NS_FINAL_CLASS macro: simply place MOZ_FINAL after the name of the class to be marked. (Unnamed classes can't be marked as final; this is a C++11 limitation.) Switching does lose us the static analysis bits, but that's actually a *good* thing. It turns out those bits were buggy, and some classes marked as "final" were inherited! Clearly static analysis tests haven't been run in awhile. Given that to the best of my knowledge no one has reported bugs about this, it's clearly better to have something some compilers will actually check -- and on many developers' machines, at that (Clang 3.0, and gcc 4.7 support final, and MSVC++2005+ supports it under the C# name "sealed") -- than to have something no one ever checks. NS_FINAL_CLASS will be removed once I get a reduced testcase to report a Clang bug that prevents the last remaining non-static-analysis-test use from being switched.
C++11 also adds the final keyword to virtual function declarations. A virtual function marked with the final keyword can't be overridden, and compilers supporting the final keyword will treat an attempt to override as an error. The MOZ_FINAL macro does double duty here:
#include "mozilla/Attributes.h"
struct B
{
virtual void foo() MOZ_FINAL;
virtual void bar() MOZ_FINAL { }
virtual void baz() MOZ_FINAL = 0;
};
Both uses of MOZ_FINAL provide some conceptual benefits in terms of reducing complexity of understanding. They also may provide some small performance benefits. Calling a final method doesn't need to incur virtual call overhead to work; calling *any* function on a final class doesn't need to incur virtual call overhead. (Although exactly how fast things will get probably depends on the linker as well as the compiler.) Bjarne Stroustrup thinks such performance micromanagement is usually "misplaced fear"[0], and regarding final he (at least once) thought "there didn't seem to be a sufficient need"[1]. But I'll let you be the judge of that for your code. :-)
Anyway, start using MOZ_FINAL where it makes sense. More detail here if you want it:
http://whereswalden.com/2011/11/26/introducing-moz_final-prevent-inheriting-from-a-class-or-prevent-overriding-a-virtual-function/
https://hg.mozilla.org/integration/mozilla-inbound/file/b5782f6da550/mfbt/Attributes.h#l174
Jeff
0.
http://www2.research.att.com/~bs/bs_faq2.html#no-derivation
1.
http://www2.research.att.com/~bs/bs_faq2.html#final