Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
5-2-3 possibly flawed
#1
Dear MISRA team,

i was wondering why is this rule advisory, when basically the same undefined behaviour applies as referenced in rule 5-2-2 (ISO 5.2.9(8))?
The result of
Code:
static_cast(&a)
is undefined if
Code:
(&a)
does not point to a B.
Granted, most compiler vendors tend to return a null pointer but it is by no means defined in the standard.
Very important: if the intention of MISRA was to prevent the usage of static_cast and allow only dynamic_cast, the rule text does not mention that at all. My suspicion rose when i studied the given example code: it shows only dynamic_cast.

BTW: am i right that rule 5-2-2 is only for old, non-ISO-compliant compilers? An attempt to use static_cast with a virtual base does not compile on any i have seen.

Thanks.
<t></t>
Reply
#2
5-2-2 is a rule to protect against undefined behaviour - hence it's required

5-2-3 is more of a style issue. Neither of the two examples shown have undefined behaviour, but arguably the 'good' version that doesn't use the cast is clearer - hence this rule is only advisory
Posted by and on behalf of
the MISRA C++ Working Group
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)