简体   繁体   中英

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

#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 :

1) A program consists of one or more translation units (Clause 2) linked together. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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