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?