Can I write something like static class
?
No, according to the C++11 N3337 standard draft Annex C 7.1.1:
Change: In C ++, the static or extern specifiers can only be applied to names of objects or functions. Using these specifiers with type declarations is illegal in C ++. In C, these specifiers are ignored when used on type declarations. Example:
static struct S { // valid C, invalid in C++ int i; };
Rationale: Storage class specifiers don’t have any meaning when associated with a type. In C ++, class members can be declared with the static storage class specifier. Allowing storage class specifiers on type declarations could render the code confusing for users.
And like struct
, class
is also a type declaration.
The same can be deduced by walking the syntax tree in Annex A.
It is interesting to note that static struct
was legal in C, but had no effect: Why and when to use static structures in C programming?