Why are classes not considered completely defined within static data member initializer?

Viewed 242

In the c++ standard it is specified that within the class member-specification (class body), the class can be considered completely-defined, but not for static data member initializer [class.mem]:

A class is considered a completely-defined object type (6.9) (or complete type) at the closing } of the class-specifier. Within the class member-specification, the class is regarded as complete within function bodies, default arguments, noexcept-specifiers, and default member initializers (including such things in nested classes). Otherwise it is regarded as incomplete within its own class member-specification.

EDIT: This is a citation from N4687, wording has changed but I do not believe the meaning changed.

I was expecting such code to compile:

struct enum_like
{
  static constexpr enum_like enum_member{};
};

Why such a definition disallowed by the C++ standard?


I believe compilers could proceed this way:

  1. read member declaration, not definition until class definition closing brace. (Now the compiler has a completely defined class)

  2. Analyse static data-member initializer (This way compilers have the constant definition of constexpr members)

  3. Analyse other member definitions.

And then resolve recursions for static member intializer as is specified in [decl.init] for non static members!

1 Answers

This rule forbids problematic things like:

struct A {
    static constexpr std::size_t N = sizeof(A);
    char buffer[N+2];
};
Related