Main Content

AUTOSAR C++14 Rule M3-1-2

Functions shall not be declared at block scope

Description

Rule Definition

Functions shall not be declared at block scope.

Rationale

It is a good practice to place all declarations at the namespace level.

Additionally, if you declare a function at block scope, it is often not clear if the statement is a function declaration or an object declaration with a call to the constructor.

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

class A {
};

void b1() {
    void func(); //Noncompliant
    A a();   //Noncompliant
}

In this example, the declarations of func and a are in the block scope of b1.

The second function declaration can cause confusion because it is not clear if a is a function that returns an object of type A or a is itself an object of type A.

Check Information

Group: Basic Concepts
Category: Required, Automated

Version History

Introduced in R2019a