MISRA Discussion Forums

Full Version: 5-2-3 possibly flawed
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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