MISRA C++:2023 Rule 7.11.3
A conversion from function type to pointer-to-function type shall only occur in appropriate contexts
Since R2024b
Description
Rule Definition
A conversion from function type to pointer-to-function type shall only occur in appropriate contexts.
Rationale
Typically, you initialize a pointer-to-function object with a function name and later use the object to call the function. Besides this usage, the rule allows a conversion from a function type to a pointer-to-function type only in these contexts:
When the conversion occurs through a
static_cast
operation.When the conversion occurs in an assignment to an object with pointer-to-function type.
In other contexts, the conversion from a function type to a pointer-to-function type can be problematic because:
The conversion can produce unintended results. For instance, if a function name is used in an arithmetic expression, the function type decays to the underlying pointer-to-function type and might produce unexpected results when the expression is evaluated.
Even if the conversion is intended, the usage can be ambiguous. For instance, if a function type is used as a Boolean, such as in
if (
, it is unclear which of the two is intended:funcName
)The function with name
must be invoked and the result of the invocation checked, but you unintentionally omitted the call operator.funcName
The pointer to the function with name
must be checked againstfuncName
nullptr
.
Polyspace Implementation
The rule checker reports a violation if an object of function-to-pointer type is used in any context other than the following:
To call a function.
As the operand of an
&
operator.As the operand of a
static_cast
.In an assignment to another object of function-to-pointer type.
Note that if a function name is used without the following parentheses, it indicates an usage of the underlying object of function-to-pointer type.
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: Standard conversions |
Category: Required |
Version History
Introduced in R2024b