Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
final means "final"
#1
Is there some hidden silliness in the spec that requires virtual methods to be declared final when the class itself is already final?  It would be redundant if so, or is my violation warning something I need to take up with the provider of our code scanner, because they got it wrong?
Reply
#2
What Autosar rule requires that?

Relevant rules I can find:

Rule A10-3-1 (required, implementation, automated)
Virtual function declaration shall contain exactly one of the three specifiers:
(1) virtual, (2) override, (3) final.

Rule A10-3-2 (required, implementation, automated)
Each overriding virtual function shall be declared with the override or
final specifier.

So you can choose to use either "final" (perhaps redundant) or "override".
Reply
#3
What I have found is that the final/virtual/override creates a chicken and egg scenario with regard to virtuals. Declaring a method final in a class that is final is itself redundant.

My scanner gives the following cyclical violation warnings

virtual method should be override or final...which leads to marking the method as override and re-running the scanner, which then says method should be final, so put both override and final on the declaration and it then complains that final must appear by itself. Trying final by itself complains that method should be override and we are back to the first case and it becomes acyclical problem.

Finally ended up disabling rules A10_3_[1..2]
Reply
#4
(18-03-2022, 05:01 AM)kent.dorfman766 Wrote: What I have found is that the final/virtual/override creates a chicken and egg scenario with regard to virtuals.  Declaring a method final in a class that is final is itself redundant.

My scanner gives the following cyclical violation warnings

virtual method should be override or final...which leads to marking the method as override and re-running the scanner, which then says method should be final, so put both override and final on the declaration and it then complains that final must appear by itself.  Trying final by itself complains that method should be override and we are back to the first case and it becomes acyclical problem.

Finally ended up disabling rules A10_3_[1..2]

Strange, our analyzer is happy with:

* Class marked "final".
* Method marked "override".
Reply
#5
10-3-2 requires that overriding virtual functions are marked as 'override' or 'final'
10-3-3 requires that overriding virtual functions of a final class should be marked 'final'
          (its not explicit, but clear from the examples - where a function marked 'override' is shown Non-Compliant)

Your static analyser should be accepting 'final' 

For the next version of MISRA C++, we are likely to revise these requirements.
Posted by and on behalf of
the MISRA C++ Working Group
Reply
#6
Are you sure A10-3-3 requires functions to be marked "final"? We've seen before that sometimes the examples do not match (or even contradict) the rule text (e.g. A3-1-5).

A10-3-3 says: "Virtual functions shall not be introduced in a final class".

Some definitions of introduce that I can find:

"to put something into use, operation, or a place for the first time" (link)
"to mention something for the first time in a piece of writing" (link)

In my view, adding an override function in a Derived class is not introducing a virtual function, since it's not the first time it appears (it did in the Base class).

Going to the original source, HIC v4.0 Rule 9.1.5, the rule is clear - do not add new virtual functions in a final class, since there won't be any further derived classes that can benefit from it. 

Code:
class Base
{
public:
   virtual void f1();
};

class Derived final : public Base
{
public:
   virtual void f2();  // Do not introduce a new virtual function!
}

Otherwise, A10-3-2 has no purpose at all, right?
Reply
#7
You are correct 10-3-3 only applies to 'introduced' functions - so B::F and B::G should actually be marked as 'rule does not apply'

Both B::F and B::G are compliant with 10-3-2 - so the Autosar example is wrong
Posted by and on behalf of
the MISRA C++ Working Group
Reply
#8
Wink 
Final means FINAL it really made me laugh!  Big Grin

One day I am working in a logistics software development company and my boss told me "'Once we've finalized the system updates, there won't be any more changes. Final means final!'" every one knows whats its mean.  Dodgy
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)