Rule A2-10-6 (required, implementation, automated)
A class or enumeration name shall not be hidden by a variable, function or enumerator declaration in the same scope.
Rationale
C++ Language Standard [3] defines that a class or enumeration name can be hidden by an explicit declaration (of the same name) of a variable, data member, function, or enumerator in the same scope, regardless of the declaration order. Such declarations can be misleading for a developer and can lead to compilation errors.
Example
//% $Id: A2-10-6.cpp 313821 2018-03-27 11:16:14Z michal.szczepankiewicz $
#include <cstdint>
namespace NS1 {
class G {};
void G() {} //non-compliant, hides class G
}
namespace NS2 {
enum class H { VALUE=0, };
std::uint8_t H = 17; //non-compliant, hides
//scoped enum H
}
namespace NS3 {
class J {};
enum H //does not hide NS2::H, but non-compliant to A7-2-3
{
J=0, //non-compliant, hides class J
};
}
int main(void)
{
NS1::G();
//NS1::G a; //compilation error, NS1::G is a function
//after a name lookup procedure
class NS1::G a{}; //accessing hidden class type name
enum NS2::H b ; //accessing scoped enum NS2::H
NS2::H = 7;
class NS3::J c{}; //accessing hidden class type name
std::uint8_t z = NS3::J;
}
See also
ISO/IEC 14882:2014 [3]: 3.3.10.2: [basic.scope.hiding] MISRA C++ 2008 [7]: 2-10-6: If an identifier refers to a type, it shall not also refer to an object or a function in the same scope. HIC++ v4.0 [9]: 3.1.1: Do not hide declarations.