MISRA C++:2023 Rule 8.2.4
Casts shall not be performed between a pointer to function and any other type
Since R2024b
Description
Rule Definition
Casts shall not be performed between a pointer to function and any other type.
Rationale
Undefined behavior can result from converting between a pointer-to-function and these kinds of pointers:
Pointer to objects
Pointer to objects of incomplete type
Pointer to
void
type (void*
)Pointer to functions of incompatible type
To indicate that a functions returned object is discarded, the returned
object is cast to void
. Casting a returned pointer-to- function to
void
for this purpose is compliant as an exception to this
rule.
This rule does not apply to standard conversions, even if they are performed by using a cast operation. The standard conversions include:
Converting a function to a pointer-to-function by implicitly taking the address of the function
Converting a pointer-to-
noexcept
-function to a pointer-to-functionConverting from
nullptr
to a pointer-to-functionConverting a pointer-to-function to
bool
type, for example, within anif
conditionUser-defined conversion, including the conversion from a captureless lambda to a pointer-to-function.
Polyspace Implementation
Polyspace® reports a violation of this rule when these nonstandard conversions are performed:
An object that is not a pointer-to-function is cast into a pointer-to-function.
A pointer-to-function is cast into an object that is not a pointer-to-function.
A pointer-to-function is cast into a pointer-to-function of incompatible type.
A pointer to a member of one class is cast to a pointer to a member of another class.
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