c++ - std::cout 在具有静态存储持续时间的对象的构造函数中的使用

标签 c++ c++11 c++14 c++03 c++98

在 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/

相关文章:

c++ - glVertexAttribDivisor 在 nVidia 上无法正常工作?

c++ - 在 LLVM libc++ 中找到的 string::find 中实现的算法(及其复杂性)是什么?

c++ - 将函数参数限制为某些枚举值

c++ - 如何为结构的数据元素赋值

c++ - 使用 Alpha Beta 剪枝提高此 MiniMax 的性能

c++ - 比较 std::functions 是否相等?

c++ - 指针分配说明

c++ - 初始化在模板化父类中定义的结构

c++ - 调用 C++ 方法指针

c++ - 强类型 using 和 typedef