Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Distinction between rule 6.8.1 and 6.8.4
#2
Your examples don't necessarily violate 6.8.1, as this requires a >use< of the reference returned by these functions after the lifetime of the A object has ended - often Undecidable.

6.8.4 is the more precautionary, but Decidable, rule that attempts to stop you generating dangling references, that could lead to violations of 6.8.1. 

Our view is that your examples 'a', 'b' and 'c' are all non-compliant with 6.8.4 , as they all return a reference to a member that might be used after the object's lifetime.

Example 'd' is compliant, as its not obvious that a() returns a reference to a member variable. There's no expectation that an analyser will analyse the behaviour of a() to ensure that a() isn't going to return a reference to a member variable by some complex process.

Example 'e' is also compliant. We'd prefer that it wasn't, because you are getting a pointer to a member variable that may lead to a 6.8.1 violation, but for simplicity the rule doesn't address this case.

We are considering the following definition of "explicitly designates" to clarify the intent:
"explicitly designates" means that a simple evaluation of the return expression in isolation, can determine if a reference to the object or sub-object is being returned, i.e. without the need to evaluate function calls or track pointers etc.
Posted by and on behalf of
the MISRA C++ Working Group
Reply


Messages In This Thread
RE: Distinction between rule 6.8.1 and 6.8.4 - by misra cpp - 23-01-2026, 02:08 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)