Main Content

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 (funcName), it is unclear which of the two is intended:

    • The function with name funcName must be invoked and the result of the invocation checked, but you unintentionally omitted the call operator.

    • The pointer to the function with name funcName must be checked against 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

expand all

#include <cstdint>
#include <iostream>

extern int32_t maybe_called ();

void use_function_name() {
    if ( 0 == maybe_called ) // Noncompliant
    {
    }
    std::cout << std::boolalpha         
              << maybe_called;          // Noncompliant

    auto op_with_func_name = +maybe_called;  // Noncompliant
}

void use_function_name_fixed() {
    if ( nullptr != &maybe_called )    // Compliant
    {
        (maybe_called)();              // Compliant
    }
}

In this example, in the function use_function_name(), the pointer-to-function maybe_called is used twice as a Boolean:

  • In an if condition expression.

  • When its underlying boolean value is printed using the std::boolalpha print format flag.

Both uses do not comply with the rule. In each case, it is ambiguous which of the following is intended:

  • The function maybe_called must be invoked and the result of the invocation used, but the call operator is omitted by mistake.

  • The underlying pointer-to-function value must be checked.

The function use_function_name_fixed() shows some compliant uses of a pointer to function.

Check Information

Group: Standard conversions
Category: Required

Version History

Introduced in R2024b