在 C++98/C++03 中具有静态存储持续时间的对象的构造函数中使用 std::cout
是否安全?
从这个看来answer它不是,但它不包含标准中的任何引用。
只有在 C++11 和 C++14 中才安全吗?
最佳答案
来自 C++14 (N3797),§27.4p2:
The objects are constructed and the associations are established at some time prior to or during the first time an object of class ios_base::Init is constructed, and in any case before the body of main begins exe- cution.295 The objects are not destroyed during program execution.296 The results of including in a translation unit shall be as if defined an instance of ios_base::Init with static storage duration. Similarly, the entire program shall behave as if there were at least one instance of ios_base::Init with static storage duration.
C++98 使用类似的术语,但没有“as if”子句。
基本上,这禁止在main
之前使用以下内容:
#include <ostream>
extern std::ostream cout;
关于c++ - std::cout 在具有静态存储持续时间的对象的构造函数中的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37706549/