Main Content

MISRA C++:2008 Rule 7-3-5

Multiple declarations for an identifier in the same namespace shall not straddle a using-declaration for that identifier

Description

Rule Definition

Multiple declarations for an identifier in the same namespace shall not straddle a using-declaration for that identifier.

Rationale

This rule requires that an identifier must not be declared in a namespace after an using declaration has already introduced an identifier with the same name. The declaration after the using statement can lead to developer confusion.

For instance, a using declaration such as:

using NS::func;
introduces the name func from the namespace NS in the current scope. If there are two overloads of func in NS, one declared before and another after the using statement, only the overload declared before is exposed to name lookup and invoked when func is called. However, in a specific call to func, a developer might expect the overload declared later to be invoked (perhaps because that overload is a better match based on function arguments).

Polyspace Implementation

The rule checker reports a violation if an identifier is declared in a namespace after these two events:

  1. Another identifier with the same name has been previously declared in the same namespace.

  2. A using declaration has already exposed that name to name lookup.

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>

namespace initialTests {
    void validateInput(uint16_t input);
}

using initialTests::validateInput;

namespace initialTests {
    void validateInput(uint32_t input); //Noncompliant
}

namespace periodicTests {
    void validateResults(uint16_t results);
    void validateResults(uint32_t results); //Compliant
}

using periodicTests::validateResults; 

void main() {
    validateInput(0U);
    validateResults(0U);
}

In this example:

  • The declaration of validateInput(uint32_t) in the namespace initialTests violates the rule because the name validateInput from this namespace has already been exposed with an using declaration. This second declaration of validateInput() can cause developer confusion. For instance, the call to validateInput() in main invokes validateInput(uint16_t) but a developer might expect validateInput(uint32_t) to be invoked because it is a better match for the call.

  • The declarations of validateResults() in the namespace periodicTests do not violate the rule because both declarations appear before the using statement. When the function validateResults() is called, all overloads of validateResults() are exposed to name lookup. A developer can use only the parameters of validateResults() in the overload declarations to decide which overload is invoked, thereby reducing chances of confusion.

Check Information

Group: Declarations
Category: Required

Version History

Introduced in R2013b