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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With