Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
9.4.2 assert(false) in default clause, compliant?
#1
Hi,

MISRA C++:2023 Rule 9.4.2 provides a number of requirements on switch statements, in particular that every branch shall be unconditionally terminated.

Assuming that assertions are always enabled (also in Release builds), would this code be compliant with 9.4.2?

Code:
enum class Foo
{
  a,
  b
};

int get(Foo f)
{
  switch(f)
  {
    case Foo::a: { return 123; }
    case Foo::b: { return 321; }
    default: { assert(false && "Unreachable"); }
  }
}

assert(false) leads to calling the assert_handler function, which is marked [[noreturn]], so it would fall under requirement 5f of the rule. Would you agree?

Thanks!
Reply
#2
If asserts are enabled, we'd say yes, using assert as the default behaviour would be acceptable 

However, in general, a static analyser cannot determine if asserts are enabled or not. Your example may still be acceptable, as the compiler may warn that not all paths return a value, but in general this is not likely to be the case.
Posted by and on behalf of
the MISRA C++ Working Group
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)