c++ - Initialize standard stream objects

前端 未结 2 787
死守一世寂寞
死守一世寂寞 2021-01-25 19:06

In what cases and why is this necessary:

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


        
2条回答
  •  一向
    一向 (楼主)
    2021-01-25 19:36

    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.

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

提交回复
热议问题