Main Content

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-function

  • Converting from nullptr to a pointer-to-function

  • Converting a pointer-to-function to bool type, for example, within an if condition

  • User-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

expand all

This example shows several casts to and from pointers to functions. The casts in the function standard_conversion() are standard conversions for pointers to functions and this rule does not apply on these casts. In the function nonstandard_conversion():

  • The pointer-to-function that takes an int is converted to a pointer-to-function that takes a char.

  • A hexadecimal integer is converted to a pointer-to-function.

  • A pointer-to-function is converted to a pointer to integer.

These are nonstandard conversions and Polyspace reports violations for these conversions.

#include <cstdint>

using funcptr_int = void (*)(int n);
using funcptr_char = void (*)(char n);
funcptr_int getFP();

void standard_conversions() {
	funcptr_int fp1 = static_cast< funcptr_int >(nullptr);
	if(fp1) {/*...*/}
}

void castToVoid(void) {
	(void) getFp();              //  Compliant by exception

}

void nonstandard_conversion() {
	// cast a function pointer to another
	// function pointer of incompatible type
	funcptr_int fp_int;
	funcptr_char fp_char = reinterpret_cast< funcptr_char >(fp_int);   // Noncompliant

	// Cast an object to function pointer
	funcptr_int fp = (funcptr_int) 0x8000;                             // Noncompliant

	// Cast a function pointer to a nonfunction pointer object
	int *ip1 = reinterpret_cast< int * >(fp_int);                      // Noncompliant
}

In the function castToVoid(), a cast to void is used to indicate that the returned value of getFP(), which is a pointer-to-function, is discarded. This type of cast is compliant with this rule.

Check Information

Group: Expressions
Category: Required

Version History

Introduced in R2024b