3.1 Scope

3.1.1 Do not hide declarations

Reusing the same identifier for different declarations is confusing and difficult to maintain. If the hiding declaration is later removed or the identifier is renamed, a compilation error may not be generated, as the declaration that was previously hidden will now be found. While hidden namespace scope identifiers can still be accesses with a fully qualified name, hidden block scope identifiers will not be accessible.

#include <cstdint>
               
void foo (int32_t);
               
int32_t i;
               
void bar (int32_t max) 
{
for (int32_t i (0); i < max; ++i)   // @@- Non-Compliant -@@
{
for (int32_t i (0); i < max; ++i) // @@- Non-Compliant -@@
{
// no way to access the outer loop index
foo (::i); // namespace scope 'i'.
foo (i);   // innermost declaration of 'i'
}
}
}

In C++, it is possible for the same identifier to refer to both a type and an object or a function. In this case the object or function will hide the type.

#include <cstdint>
               
// valid C++
class C;
int32_t C;   // @@- Non-Compliant: object C hides type of same name -@@

References

  • HIC++ v3.3 - 8.2.1

Click here for references