std :: cout在具有静态存储持续时间的对象的构造函数中的用法 [英] std::cout usage in constructors of objects with static storage duration
问题描述
在C ++ 98 / C ++ 03中使用statc存储持续时间的对象的构造函数中使用 std :: cout
是否安全?
Is it safe to use std::cout
in constructors of objects with statc storage duration in C++98 / C++03?
从这个回答看来,它不是,但不包括任何
It seems from this answer that it isn't but it doesn't include any quotes from the standard.
在C ++ 11和C ++ 14中只能安全吗?
Is it only safe to do that in C++11 and C++14?
推荐答案
从C ++ 14(N3797),§27.4p2:
From C++14 (N3797), §27.4p2:
并且在第一个
时间之前的某个时间建立关联,并且在任何情况下在主体的主体开始exe-
cution之前构建ios_base :: Init类的对象。 295在程序执行期间对象不会被销毁。在翻译单元中包含
的结果应该好像定义了一个带有静态存储
持续时间的ios_base :: Init的实例。类似地,整个程序应当表现为具有静态存储持续时间的至少一个ios_base :: Init
实例。
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子句。
C++98 uses similar terminology, but without the "as if" clause.
基本上,这 主
:
#include <ostream>
extern std::ostream cout;
这篇关于std :: cout在具有静态存储持续时间的对象的构造函数中的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!