After reading the static variables in the built-in function, I wrote this test program:
main.cpp:
#include <iostream>
#include "a.h"
#include "f.h"
void g();
int main()
{
std::cout << "int main()\n";
f().info();
g();
}
hijras:
struct A
{
A() { std::cout << "A::A()\n"; }
void info() { std::cout << this << '\n'; }
};
fh: (singleton local for each compilation unit due to unnamed namespace)
namespace {
inline A& f()
{
static A x;
return x;
}
}
g.cpp:
#include <iostream>
#include "a.h"
#include "f.h"
void g()
{
std::cout << "void g()\n";
f().info();
}
The problem is that I do not get the same results with different compilers:
g ++ 4.8.2: OK
int main()
A::A()
0x6014e8
void g()
A::A()
0x6014d0
clang ++ 3.7.0: OK
int main()
A::A()
0x6015d1
void g()
A::A()
0x6015c1
icpc 15.0.2: no call to A :: A () inside g ()!
int main()
A::A()
0x601624
void g()
0x601620
Is this a bug in icpc? Is the program behavior defined? If I replaced "namespace {...}" with "static" in fh, A :: A () is called inside g (), as expected. Shouldn't the behavior be the same?
"namespace {...}" fh, A:: info() main() g(), A:: A() ( ), .