std::cout 在具有静态存储持续时间的对象的构造函数中的用法
std::cout usage in constructors of objects with static storage duration
在 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++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;