是一个声明void();法律,这实际上是什么? [英] Is a statement void(); legal and what is it actually?
问题描述
我有一小段代码,其中有一条语句void();
I have a little piece of code which has a statement void();
int main()
{
void( ); // 1: parses fine in GCC 5.4.0 -Wpedantic
// void; // 2: error declaration does not declare anything
}
1 void()
到底是什么?
- 匿名函数声明?
- 类型声明?
- 一个空表达式?
什么使1 void()
与2 void;
不同?
What makes 1 void()
different from 2 void;
?
我已经阅读:
- sizeof(void())是合法表达式吗?,但void()被认为是sizeof
- decltype(void())中的void()到底是什么意思?在declspec中被认为是什么?
- 我读到 void {}是否合法?
- Is sizeof(void()) a legal expression? but there void() is considered a type in sizeof
- What does the void() in decltype(void()) mean exactly? where it is considered in declspec.
- And I read Is void{} legal or not?
但是我很好奇这个松散的声明是否为void();与其中之一不同(当然还有原因)
But I am curious if the loose statement void(); is different from one of those (and why of course)
推荐答案
void;
是错误的,因为语言语法中没有与该代码匹配的规则.特别是,没有规则 type-id
;
,
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
;
,
但是,代码void()
匹配两个语法规则:
However, the code void()
matches two grammar rules:
-
type-id
. -
postfix-expression
,子情况为simple-type-specifier
(
expression-list-opt
)
.
type-id
.postfix-expression
, with the sub-case beingsimple-type-specifier
(
expression-list-opt
)
.
现在,解析器需要将void();
与语法规则匹配.即使void()
匹配 type-id
,如前所述,也没有规则匹配 type-id
;
.因此,在这种情况下,解析器拒绝将void()
解析为 type-id
,并尝试其他可能性.
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.
有 一系列规则,这些规则定义 postfix-expression
;
进行声明.因此,在这种情况下,void()
被明确地解析为 postfix-expression .
There is a series of rules defining that postfix-expression
;
makes a statement. So void()
is unambiguously parsed as postfix-expression in this context.
正如您已链接的其他答案所描述的那样,此代码作为 postfix-expression 的语义是void
类型的prvalue.
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
.
相关链接: sizeof(int())
是合法表达吗?
Related link: Is sizeof(int())
a legal expression?
这篇关于是一个声明void();法律,这实际上是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!