Rule A7-1-9 (required, implementation, automated)

A class, structure, or enumeration shall not be declared in the definition of its type.

Rationale

Combining a type definition with a declaration of another entity can lead to readability problems and can be confusing for a developer.

Example

// $Id: A7-1-9.cpp 305629 2018-01-29 13:29:25Z piotr.serwa $
#include <cstdint>

enum class DIRECTION
{
UP,
DOWN
} dir;
//non-compliant

class Foo
{
public:
enum class ONE {AA, BB}; //compliant

static constexpr enum class TWO {CC, DD} sVar = TWO::CC; // non-compliant
static constexpr ONE sVar2 = ONE::AA; //compliant

};

struct Bar
{
std::uint32_t a;
} barObj; //non-compliant

struct Bar2
{
std::uint32_t a;
} bar2Obj, *bar2Ptr; //non-compliant, also with A7-1-7

struct Foo2
{
std::uint32_t f;
};

Foo2 foo2Obj; //compliant

See also

JSF December 2005 [8]: AV Rule 141: A class, structure, or enumeration will not be declared in the definition of its type. C++ Core Guidelines [11]: C.7: Don’t define a class or enum and declare a variable of its type in the same statement.