Rule Definition
C-style (cast notation), and functional notation casts that do not invoke a converting constructor are capable of performing casts between unrelated types.
Also, C++ style casts clearly communicate the intent and hence improve readability.
Remediation
Use C++ style casts:
static_cast(expression)
const_cast(expression)
dynamic_cast(expression)
reinterpret_cast(expression)
Violation Code Sample
class A
{
public:
explicit A( int32_t );
};
int32_t g ( )
{
return 7;
}
void f ( )
{
A * a2 = ( A* )( &a1 ); // Non-compliant
}
Fixed Code Sample
(void)g ( ); // Compliant by exception
A * a3 = const_cast<A*>( &a1); // compliant
Reference
Standards References:
MISRA C++ 2008, 5-2-4: C-style casts (other than void casts) and functional notation casts (other than explicit constructor calls) shall not be used.
Related Technologies
Technical Criterion
CWE-704 - Incorrect Type Conversion or Cast
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.