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.1
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
usingdeclaration 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
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.