Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is std::cout guaranteed to be initialized?

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?

like image 906
Inbae Jeong Avatar asked Jan 09 '12 06:01

Inbae Jeong


3 Answers

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.

like image 89
GManNickG Avatar answered Oct 07 '22 21:10

GManNickG


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.

like image 38
Guillaume Paris Avatar answered Oct 07 '22 21:10

Guillaume Paris


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.

like image 2
Basile Starynkevitch Avatar answered Oct 07 '22 20:10

Basile Starynkevitch