C ++翻译单元的语法 [英] Grammar of a C++ Translation Unit

查看:168
本文介绍了C ++翻译单元的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,在很长时间之后,是一个C ++翻译单元,在预处理器运行之后,是一个声明序列(让我提醒任何定义也是一个声明)。

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).

许多人都在使用这个语句,但没有人曾经给过一个反例。但我自己发现这个例子困扰我:

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

这与MSVC和online comeau编译良好。我知道标准定义了 空语句 ,但我从来没有听说过空声明。所以,我看到三个选项:

This compiles fine with MSVC and online comeau. I know the standard defines an empty statement but I never heard of an empty declaration. So, I see three options:


  • 我的理解正确,标准定义了一个空声明

  • 我的理解是正确的,但是标准没有定义空声明,并且上面的翻译是错误的

  • 我的理解是不正确的,即一个C ++ TU不是一个声明序列

  • My understanding is correct and the standard defines an empty declaration
  • My understanding is correct but the standard doesn't define empty declarations and the above translation is ill-formed
  • My understanding is incorrect, i.e. a C++ TU is not a sequence of declarations

请帮我解开我的疑惑。感谢

Please help me dissolve my doubts. Thanks

推荐答案

在C ++ 0x的当前草稿中允许使用空声明在文件范围(和命名空间范围和其他允许声明的地方),它只是一个分号。它是一个独立的语法实体。

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. It is a standalone grammatical entity.

在C ++ 03中,不允许只有一个声明的单独的分号。虽然可能会出现一个简单声明可能只能减少到一个分号,但显式规则不允许这样做。

In C++03 a lone semicolon is not allowed where only a declaration is expected. 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


在简单声明中,可以省略可选的 init-declarator-list 当声明一个类(第9节)或枚举(7.2)时,即当 decl-specifier-seq 包含 class-specifier 类型说明符与类键(9.1)或枚举说明符

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.

简而言之,这意味着只有当未省略 decl-specifier-seq 时,才可以省略 init-declarator-list

In short this implies that the init-declarator-list can be omitted only when the decl-specifier-seq is not omitted.

这篇关于C ++翻译单元的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆