CAST-old-style (C++ only)
Synopsis
Old style casts (other than void casts) are used
Enabled by default
No
Severity/Certainty
Medium/Medium

Full description
Old style casts (other than void casts) are used. These casts override type information about the variables or pointers being cast, which might cause portability problems. A particular cast might for example not be valid on a system, but the compiler will perform the cast anyway. The new style casts static_cast, const_cast, and reinterpret_cast should be used instead because they make clear the intention of the cast. Moreover, the new style casts can easily be searched for in source code files, unlike old style casts. This check is identical to MISRAC++2008-5-2-4, MISRAC++2023-8.2.2.
Coding standards
- CERT EXP05-CPP
Do not use C-style casts
- MISRA C++ 2008 5-2-4
(Required) C-style casts (other than void casts) and functional notation casts (other than explicit constructor calls) shall not be used.
- MISRA C++ 2023 8.2.2
(Required) C-style casts and functional notation casts shall not be used
Code examples
The following code example fails the check and will give a warning:
int example(float b)
{
return (int)b;
}
The following code example passes the check and will not give a warning about this issue:
int example(float b)
{
return static_cast<int>(b);
}