MISRA.FUNC_CASTFunctional notation cast different from explicit constructor call MISRA-C++ Rule 5-2-4 (required): C-style casts (other than void casts) and functional notation casts (other than explicit constructor calls) shall not be used.This rule is also covered by MISRA.C_CAST. RationaleC-style (cast notation), and functional notation casts that do not invoke a converting constructor are capable of performing casts between unrelated types. ExceptionA C-style cast to void may be used to signify that the return value for a non-void function call is being ignored (see Rule 0—1—7). Exampleclass A { public: explicit A( int32_t ); }; int32_t g ( ) { return 7; } void f ( ) { A const a1 = A( 10 ); // Compliant A * a2 = ( A* )( &a1 ); // Non-compliant A * a3 = const_cast<A*>( &a1 ); // Compliant, but breaks Rule 5—2—5 (void)g ( ); // Compliant by exception } In the above example, the C-style cast from 'a1' to a non-const pointer is stronger than necessary. If the type of 'a1' is changed at some future date, then the cast may continue to compile. |