Does the standard (as of C++20) explicitly or implicitly allow using a different class-key when (forward-)declaring a class-name than when defining it?
For the purpose of this question class-key shall be limited to class
or struct
, excluding union
.
In other words, is this legal:
struct C;
class C{};
class S;
struct S{};
An answer should explicitly refer to the C++20 standard (or a suitable draft).
I couldn't find anything in the entire [class] section.
The only indication I could find is in [decl.type.elab.6] which first states:
The class-key or enum keyword present in the elaborated-type-specifier shall agree in kind with the declaration to which the name in the elaborated-type-specifier refers.
But to me it is unclear what kind means in this context. I suppose enum
and struct
are different kinds but are struct
and class
different kinds? I don't know. Anyway after some unrelated points about friends it goes on to say:
Thus, in any elaborated-type-specifier, the enum keyword shall be used to refer to an enumeration ([dcl.enum]), the union class-key shall be used to refer to a union ([class.union]), and either the class or struct class-key shall be used to refer to a non-union class ([class.pre]).
(emph added)
This sort of sounds as if struct
and class
were interchangeable, but I'm not sure about it.
According to code explorer it appears to be generally accepted by compilers.
Non-Duplicates
I do not believe Mixing class and struct answers the question. Due to its age, it talks about a different version of the standard. Also, the one answer that actually quotes the standard does not convince: The quoted paragraph has a lot of ambiguity and does not address the question asked here.
Forward declaration as struct vs class has no answers actually bothering to prove their assertions by citation.
Changing struct to class (and other type changes) and ABI/code generation Asks about two definitions with different class-keys. Its answer does not apply here.