简体   繁体   English

在c ++中,仅在全局范围内允许使用“表达式”来初始化全局对象。 在标准中哪里可以找到?

[英]In c++, `expressions` are allowed in global scope only to initialize global objects. Where can I find this in the Standard?

The commented out expression below doesn't compile, because it is in the global scope. 下面的注释表达式不能编译,因为它在全局范围内。 But where in the C++ Standard do I find something that relates to this restriction? 但是在C ++标准的哪里可以找到与此限制有关的内容? As I recall, expressions are allowed in global scope only to initialize global objects. 我记得,在全局范围内仅允许表达式初始化全局对象。 I have already looked into Chapter 5 Expressions , to no avail. 我已经研究了第5章表达式 ,但无济于事。

#include <iostream>
//std::cout.setf(std::ios_base::hex, std::ios_base::basefield);


int main()
{
    std::cout.setf(std::ios_base::hex, std::ios_base::basefield);
} 

I believe it is all going down to 3.5 Program and linkage : 我相信一切都将取决于3.5程序和链接

1) A program consists of one or more translation units (Clause 2) linked together. 1)一个程序由一个或多个链接在一起的翻译单元(第2条)组成。 A translation unit consists of a sequence of declarations. 翻译单元由一系列声明组成。
translation-unit: 翻译单位:
declaration-seq opt 声明顺序选择

Following that syntax description you'll find what is allowed in a declaration-seq and what is allowed in each declaration . 在该语法说明之后,您将找到一个声明序列中允许的内容以及每个声明中允许的内容。 And as can be seen a regular statement is not allowed at translation-unit level. 可以看出,在翻译单位级别不允许使用常规语句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM