Main Content

AUTOSAR C++14 Rule M5-2-6

A cast shall not convert a pointer to a function to any other pointer type, including a pointer to function type

Description

Rule Definition

A cast shall not convert a pointer to a function to any other pointer type, including a pointer to function type.

Rationale

Because calling a function through a pointer with incompatible type results in undefined behavior, the rule forbids these cast operations:

  • Casting from a function pointer to any other type.

  • Casting from a function pointer to another function pointer, if the function pointers have different arguments and return types.

Polyspace Implementation

Polyspace® raises a violation of this rule if the source type of a cast operation is a pointer to a function.

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

void* (*func1) (int);
void* (*func2)(float);
void bar(void* (*)(int));
void foo(){
	
	bar(reinterpret_cast<void* (*) (int)> (func2)); //Noncompliant
	bar(func1);
}

In this example, the function bar accepts a pointer to a function that takes an int as an input and returns a void* pointer. In foo(), the function pointer func2 is converted so that it can be passed to bar. Polyspace flags the noncompliant conversion of function pointers.

Check Information

Group: Expressions
Category: Required, Automated

Version History

Introduced in R2019a