MISRA Discussion Forums
5-2-3 possibly flawed - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: MISRA C++:2008 rules (https://forum.misra.org.uk/forumdisplay.php?fid=19)
+---- Forum: 6.5 Expressions (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=134)
+---- Thread: 5-2-3 possibly flawed (/showthread.php?tid=1379)



5-2-3 possibly flawed - dg1980 - 19-12-2017

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.


Re: 5-2-3 possibly flawed - misra cpp - 17-01-2018

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