What does static class mean?

What does this construction mean

static class Class {};

If the design doesn't make sense then why does the studio compile it.

Author: Stanislav Petrov, 2018-05-29

2 answers

The meaning can appear if a variable is defined together with the class definition, that is, the code is transformed into the following:

static class Class {} var;

This will mean that a variable of the newly defined class is being created. The fact that Visual Studio allows such a construction, i.e. it only issues a warning, and not a compilation error, is a feature of the compiler.

Moreover, var, depending on the context, can be either a definition of a global variable that is available only within the current file, if it is written at the global namespace level. Or it can be a declaration of a static variable of the enclosing class, if written inside the class definition:

struct X {
    static class Class {} var;
};

In this case, to use var, it will still need to be defined outside X:

X::Class X::var;
 3
Author: αλεχολυτ, 2018-05-29 15:52:13

Such a construction means nothing - it is not compiled in principle. C++ has always allowed storage class specifiers (static in this case) only in object or function declarations.

10.1.1 Storage class specifiers
4 The static specifier can be applied only to names of variables and functions and to anonymous unions [...]

Http://eel.is/c++draft/dcl.stc#4

This construction does not declare any no object, no function. Any C++ compiler is required to issue a diagnostic message in response to such a construction.

The concept of "compiles" or "does not compile" does not exist in the C++ world. The correctness of C++ code is determined primarily by the presence or absence of compiler diagnostic messages.

In this case, the compiler's decision to continue compiling after issuing a diagnostic message may be caused by the legacy of the C language, in which such pointless storage class specifiers are valid (though also meaningless).

 3
Author: AnT, 2018-05-29 15:56:09