void;
is an error because there is no rule in the language grammar which matches that code. In particular, there is no rule type-id
;
,
However, the code void()
matches two grammar rules:
type-id
.
postfix-expression
, with the sub-case being simple-type-specifier
(
expression-list-opt
)
.
Now, the parser needs to match void();
to a grammar rule. Even though void()
matches type-id
, as mentioned earlier there is no rule that would match type-id
;
. So the parser rejects the possible parsing of void()
as type-id
in this context, and tries the other possibility.
There is a series of rules defining that postfix-expression
;
makes a statement. So void()
is unambiguously parsed as postfix-expression in this context.
As described by the other answers you linked already, the semantic meaning of this code as a postfix-expression is a prvalue of type void
.
Related link: Is sizeof(int())
a legal expression?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…