[英]Grammar of a C++ Translation Unit
My understanding, for a long time now, was that a C++ translation unit , after the preprocessor has run, is a sequence of declarations (let me remind that any definition is also a declaration). 我很长一段时间以来的理解是,在预处理器运行之后, C ++转换单元 是一系列声明 (让我提醒任何定义也是一个声明)。
Many people have argued with this statement but no one has ever given a counterexample. 很多人都赞同这个说法,但没有人给出反例。 But I myself found this example which troubles me: 但我自己发现这个让我烦恼的例子:
int x; //declaration
; // ??? EMPTY DECLARATION?
int main() //dec
{ //la
} //ration
This compiles fine with MSVC and online comeau. 这与MSVC和在线照片很好地编辑。 I know the standard defines an empty statement but I never heard of an empty declaration. 我知道标准定义了一个空语句,但我从未听说过空声明。 So, I see three options: 所以,我看到三个选择:
Please help me dissolve my doubts. 请帮我解除疑惑。 Thanks 谢谢
Your understanding is correct and the standard (or at least Stroustrup) does define an empty declaration
.
您的理解是正确的,标准(或至少Stroustrup)确实定义了一个空声明
。
EDIT : It seems this answer is wrong (there's a semantic rule on the standard - but not on the book, as far as I can tell - that prohibits both decl-specified-seq
and init-declarator-list
of being empty at the same time). 编辑 :似乎这个答案是错误的(标准上有一个语义规则 - 但据我所知,这本书没有 - 禁止decl-specified-seq
和init-declarator-list
为空时间)。 See Charles Bailey's answer. 请参阅Charles Bailey的回答。
n "The C++ Programming Language", appendix A, section A.4: n“C ++编程语言”,附录A,A.4节:
A program is a collection of
translation-unit
s (...). 程序是translation-unit
的集合(...)。 Atranslation-unit
, often called a source file , is a sequence ofdeclaration
s:translation-unit
(通常称为源文件 )是一系列declaration
:
translation-unit:
declaration-seq_opt
opt
means the production is optional. opt
意味着生产是可选的。 In this rule, it means an empty translation unit is valid. 在此规则中,它表示空翻译单元有效。
Section A.7: 第A.7节:
declaration-seq:
declaration
declaration-seq declaration
declaration:
block-declaration
(...)
block-declaration:
simple-declaration
(...)
simple-declaration:
decl-specified-seq_opt init-declarator-list_opt ;
So declaration-seq
is a sequence of at least one declaration
. 因此, declaration-seq
是至少一个declaration
的序列。 A declaration
can, amongst other things, be a block-declaration
, which in turn produces simple-declaration
. 除其他事项外, declaration
可以是block-declaration
,而block-declaration
又可以生成simple-declaration
。 As both the decl-specified-seq
and init-declarator-list
non-literals are optional, ;
同时作为decl-specified-seq
和init-declarator-list
非文字是可选的, ;
is a valid declaration. 是一个有效的声明。
An empty-declaration is allowed in (the current draft of) C++0x at file scope (and namespace scope and other places where a declaration is allowed) and it is just a semicolon. 在文件范围(和命名空间范围以及允许声明的其他位置)的C ++ 0x的(当前草案)中允许空声明 ,它只是一个分号。 It is a standalone grammatical entity. 它是一个独立的语法实体。
In C++03 a lone semicolon is not allowed where only a declaration is expected. 在C ++ 03中,只允许声明,不允许使用单独的分号。 Although it might appear that a simple-declaration might be able to reduce to just a semicolon an explicit rule disallows this. 虽然看起来简单声明可能只能缩减为分号,但显式规则不允许这样做。
7 [dcl.dcl] / 3 7 [dcl.dcl] / 3
In a simple-declaration, the optional init-declarator-list can be omitted only when declaring a class (clause 9) or enumeration (7.2), that is, when the decl-specifier-seq contains either a class-specifier , an elaborated-type-specifier with a class-key (9.1), or an enum-specifier . 在一个简单声明中,只有在声明一个类(第9节)或枚举(7.2)时,也就是说,当decl-specifier-seq包含一个类说明符时 ,可以省略可选的init-declarator-list , 详细说明-type-specifier ,带有类键 (9.1)或枚举说明符 。
In short this implies that the init-declarator-list can be omitted only when the decl-specifier-seq is not omitted. 简而言之,这意味着只有在不省略decl-specifier-seq时才能省略init-declarator-list 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.