According to C++11 specification:
The results of including
<iostream>
in a translation unit shall be as if<iostream>
defined an instance ofios_base::Init
with static storage duration. Similarly, the entire program shall behave as if there were at least one instance ofios_base::Init
with static storage duration
This implies that if my code looks like that:
// A.cpp
#include <iostream>
using namespace std;
unsigned long foo() {
cerr << "bar";
return 42;
}
and
// B.cpp
using namespace std;
extern unsigned long foo();
namespace {
unsigned long test() {
int id = foo();
return id;
}
unsigned long id = test();
}
int main() {
return 0;
}
then I should be safe calling cerr
without the risk of a static initialization fiasco.
Unfortunately, that code segfaults... Why? I don't think gcc 6.2.1 decided to ignore the C++11 specification, and I included <iostream>
in A.cpp. According to the specification it should suffice.