MISRA C++:2008 Rule 5-2-4
C-style casts (other than void casts) and functional notation casts (other than explicit constructor calls) shall not be used
Description
Rule Definition
C-style casts (other than void casts) and functional notation casts (other than explicit constructor calls) shall not be used.
Rationale
C-style casts are difficult to distinguish in the source code. The developer intent in casting from one type to another is not communicated. Code that contains C-style casts are difficult to understand and debug. Avoid C-style casts.
C++ introduces explicit casting operations that are easily identified and that clearly communicate the developer intent behind each cast. Use these casting operations instead.
Polyspace Implementation
Polyspace® flags c-style casts in your code. Compliant C++ style casting operations include:
std::static_cast
std::reinterpret_cast
std::const_cast
std::dynamic_cast
Cast by using
{}
notation
In addition, the reference_cast operation from the boost library and the safe_cast operation from the Miscrosoft library are also allowed.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Expressions |
Category: Required |
Version History
Introduced in R2013b