From 17ccab5836bfc3b8cdecd1bff3c4fae1a446271c Mon Sep 17 00:00:00 2001 From: Bjarne Stroustrup Date: Tue, 16 May 2017 13:28:23 -0400 Subject: [PATCH] Fix C.139 --- CppCoreGuidelines.md | 40 ++++++---------------------------------- 1 file changed, 6 insertions(+), 34 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index f207a14..54d6498 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -1,6 +1,6 @@ # C++ Core Guidelines -May 8, 2017 +May 16, 2017 Editors: @@ -7046,7 +7046,6 @@ Diagnose name hiding ##### Reason Capping a hierarchy with `final` is rarely needed for logical reasons and can be damaging to the extensibility of a hierarchy. -Capping an individual virtual function with `final` is error-prone as that `final` can easily be overlooked when defining/overriding a set of functions. ##### Example, bad @@ -7057,38 +7056,6 @@ Capping an individual virtual function with `final` is error-prone as that `fina class My_improved_widget : public My_widget { /* ... */ }; // error: can't do that -##### Example, bad - - struct Interface { - virtual int f() = 0; - virtual int g() = 0; - }; - - class My_implementation : public Interface { - int f() override; - int g() final; // I want g() to be FAST! - // ... - }; - - class Better_implementation : public My_implementation { - int f(); - int g(); - // ... - }; - - void use(Interface* p) - { - int x = p->f(); // Better_implementation::f() - int y = p->g(); // My_implementation::g() Surprise? - } - - // ... - - use(new Better_implementation{}); - -The problem is easy to see in a small example, but in a large hierarchy with many virtual functions, tools are required for reliably spotting such problems. -Consistent use of `override` would catch this. - ##### Note Not every class is meant to be a base class. @@ -7097,6 +7064,11 @@ This rule is about using `final` on classes with virtual functions meant to be i ##### Note +Capping an individual virtual function with `final` is error-prone as `final` can easily be overlooked when defining/overriding a set of functions. +Fortunately, the compiler catches such mistakes: You cannot re-declare/re-open a `final` member a derived class. + +##### Note + Claims of performance improvements from `final` should be substantiated. Too often, such claims are based on conjecture or experience with other languages.