Is std::cout guaranteed to be initialized?

你说的曾经没有我的故事 提交于 2019-12-17 04:07:25

问题


What I know about C++ is that the order of the constructions (and destructions) of global instances should not be assumed.

While I'm writing code with a global instance which uses std::cout in the constructor & destructor, I got a question.

std::cout is also a global instance of iostream. Is std::cout guaranteed to be initialized before any other global instances?

I wrote a simple test code and it works perfectly, but still I don't know why.

#include <iostream>

struct test
{
    test() { std::cout << "test::ctor" << std::endl; }
    ~test() { std::cout << "test::dtor" << std::endl; }
};

test t;

int main()
{
    std::cout << "Hello world" << std::endl;
    return 0;
}

It prints

test::ctor
Hello world
test::dtor

Is there any possibility that the code doesn't run as expected?


回答1:


The answer differs depending on if you're using C++03 or C++11.

In C++11, your code is guaranteed to work, but in C++03 it's unspecified; your only guarantee is that by the time main() is entered, the standard streams had been initialized. (That said, all mainstream implementations initialize them prior to running any dynamic initialization, making them fine to use.)

You can force initialization by constructing an std::ios_base::Init object, like so:

#include <iostream>

struct test
{
    test() { std::cout << "test::ctor" << std::endl; }
    ~test() { std::cout << "test::dtor" << std::endl; }

private:
    std::ios_base::Init mInitializer;
};

test t;

int main()
{
    std::cout << "Hello world" << std::endl;
    return 0;
}

Now when test constructs, it initializes mInitializer and guarantees the streams are ready to use.

C++11 fixed this slightly annoying behavior by acting as if every instance of #include <iostream> were followed by static std::ios_base::Init __unspecified_name__;. This automatically guarantees the streams are ready to use.




回答2:


According to §27.3/2:

The objects [std::cin, std::cout, etc.] are constructed, and the associations are established at some time prior to or during first time an object of class ios_base::Init is constructed, and in any case before the body of main begins execution.




回答3:


Your question is about the order of construction of static objects. I believe that the language specification leaves it undefined.

GCC has the init_priority attribute to play with the order.

And I believe you should not worry that much in practice.



来源:https://stackoverflow.com/questions/8784892/is-stdcout-guaranteed-to-be-initialized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!