Main Content

AUTOSAR C++14 Rule A2-10-4

The identifier name of a non-member object with static storage duration or static function shall not be reused within a namespace

Since R2020b

Description

Rule Definition

The identifier name of a non-member object with static storage duration or static function shall not be reused within a namespace.

Rationale

You use namespaces to narrow the scope of the identifiers that you declare within these namespaces. This prevents these identifiers from being mistaken with identical identifiers in other scopes. If you reuse an identifier with static storage duration within the same namespace across source files, you might mistake one identifier for the other.

Polyspace Implementation

  • When you reuse identifiers, Polyspace® flags the last use of the identifier if they are in the same translation unit. If the identifiers are in separate files, the identifier in the last file path by alphabetical order is flagged.

    However, if you reuse an identifier but declare only one instance of the identifier with the keyword static, that identifier is flagged regardless of the order in which the identifiers are declared.

  • Polyspace raises no violation if you declare an identifier in a namespace and you reuse that identifier in the same namespace, but within a nested or inlined namespace. For instance, no violation is raised on reusedVar in this code snippet.

    //file1.cpp
    namespace foo {
      static int reusedVar; //resuedVar has static storage duration
    }
    
    //file2.cpp
     namespace foo {
      void func();
      namespace nested_foo { 
        float reusedVar;
      }
      inline namespace inlined_foo {
        char reusedVar;
      }
    }
        

The checker is not raised on unused code such as

  • Noninstantiated templates

  • Uncalled static or extern functions

  • Uncalled and undefined local functions

  • Unused types and variables

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

file1.cpp

#include <cstdint>

namespace first_namespace
{
	static std::int32_t global_var; //Compliant- Reused in global namespace
	static std::int32_t reusedVar1;  //Noncompliant
	void reusedVar2();
	static std::int32_t reusedVar3; //Noncompliant
	void use(){
		++reusedVar1;
		reusedVar2();
		++reusedVar3;
	}
}
static std::int32_t file_var = 10; //Compliant - identifier not reused

file2.cpp

#include <cstdint>



static std::int32_t global_var; //Compliant - Reused in global namespace



namespace first_namespace
{
	std::int32_t reusedVar1;
	static std::int32_t reusedVar2;  //Noncompliant
	void f()
	{
		float reusedVar3;
		++reusedVar3;
	}
	void otherUse() {
		++reusedVar2;
	}
}



namespace second_namespace
{
	std::int32_t reusedVar1; //Compliant - Reused in different namespace
}

In this example, Polyspace flags the reuse of resusedVar1, reusedVar2, and reusedVar3 in the same namespace in both files. Polyspace does not flag the reuse of reusedVar1 in a different namespace in file2.cpp. Note that when only one instance of the reused identifier is declared with the keyword static, Polyspace flags that instance. The identifier global_var is not flagged because it is declared in different namespaces, the global namespace, and first_namespace.

Check Information

Group: Lexical conventions
Category: Required, Automated

Version History

Introduced in R2020b