c ++ - 初始化标准流对象 [英] c++ - Initialize standard stream objects

查看:123
本文介绍了c ++ - 初始化标准流对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在什么情况下以及为什么这是必要的:

In what cases and why is this necessary:

class ios_base::Init {
    static int init_cnt;  // internal static counter (for exposition only)
public:
    Init();
    ~Init();
}

从这个网站获取: cpluplus.com - ios_base :: init

我读了描述和沉思,但这没有多大帮助...

I read the description and meditated about it, but this did not help much...

推荐答案

这是静态初始化顺序惨败。本质上,如果要使用静态初始化程序中的全局流对象,可以调用它来确保它们都是及时构造的。我熟悉的运行时已经正确地做到了这一点,但从技术上来说这是不能保证的。

It's a workaround for the static initialization order fiasco. Essentially, if you want to use the global stream objects from a static initializer, you can call this to ensure that they're all constructed in time. Runtimes I'm familiar with already do this properly, but technically speaking that's not guaranteed.

(另请注意,从C ++ 11开始,保证。)

(Note also that as of C++11, it is guaranteed.)

这篇关于c ++ - 初始化标准流对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆