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;
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:
Another identifier with the same name has been previously declared in the same namespace.
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
Check Information
Group: Declarations |
Category: Required |
Version History
Introduced in R2013b