MISRA C++:2023 Rule 8.2.5
Description
Rule Definition
reinterpret_cast
shall not be used.
Rationale
The operator reinterpret_cast
allows casting between unrelated
types, which can lead to undefined behavior. To avoid undefined behavior, do not use
reinterpret_cast
.
As an exception, reinterpret_cast
is allowed in these cases
because the behavior of these conversions are well-defined:
Casting a pointer to
T*
, whereT
is one ofvoid
,char
,unsigned char
, orstd::byte
, with cv qualification.Converting a pointer
p
to an integer that is large enough to represent a pointer value, such asstd::uintptr_t
.
Polyspace Implementation
Polyspace® reports a violation of this rule on use of the
reinterpret_cast
operator. As exceptions, these uses are not
reported as violations:
reinterpret_cast<T*>
whereT
is one ofvoid
,char
,unsigned char
orstd::byte
.reinterpret_cast<T>(p)
wherep
is a pointer andT
is an integer that is large enough to represent a pointer value.
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 R2024b