AutosarC++18_03-A2.10.6

A class or enumeration name shall not be hidden by a variable, function or enumerator declaration in the same scope

Required inputs: IR

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 NS1

namespace NS2 {
enum class H {
  VALUE = 0,
};
std::uint8_t H = 17; // non-compliant, hides
                     // scoped enum H
} // namespace NS2

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
};
} // namespace NS3

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 [6]: 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 [8]: 3.1.1: Do not hide declarations.

Possible Messages

Key

Text

Severity

Disabled

reused_type

Type should not be hidden by a name in the same scope.

None

False

Options

invert_findings

invert_findings : bool = True

Whether to invert primary and secondary SLocs for violations