在iostream头中使用静态数据

时间:2016-11-10 12:57:42

标签: c++ iostream

我在 godbolt.org 上玩,并注意到编译器添加了一些"extra code",其中带有{em>'空主()' {{1}包括标题。

在研究标准后,我认为需要构建和初始化对象iostreamcincoutcerrclog,{{ 1}},wcinwcout,如果它们尚未构建/初始化。

N4606:§27.5.3.1.6

wcerr

但是对于wclog,数据,所有标准都表示它计算27.5.3.1.6 Class ios_base::Init [ios::Init] namespace std { class ios_base::Init { public: Init(); ~Init(); private: static int init_cnt; // exposition only }; } 的构造函数和析构函数调用的数量,并初始化为零。

  

2为了说明,这里提供了维护数据   as:(2.1) - static int init_cnt,计算构造函数的数量和   析构函数调用类Init,初始化为零。

我没有得到的是static在这里的用途是什么? 为什么我们需要计算构造函数/析构函数的调用次数?

1 个答案:

答案 0 :(得分:4)

基于https://github.com/maniacbug/StandardCplusplus/blob/master/ios.cpp#L163我认为如果需要那么在调用全局变量的析构函数时最后编程可以正确地破坏cin,cout,cerr,clog,wcin,wcout,wcerr和wclog。

来自http://www.csci.csusb.edu/dick/c++std/september/lib-iostreams.html

  

〜初始化();

     

效果:       销毁类Init的对象。该函数从中减去一个       存储在init_cnt中的值,如果得到的存储值是       一,调用cout.flush(),cerr.flush(),clog.flush(),wcout.flush(),       wcerr.flush(),wclog.flush()。