Possible Duplicate:
(static initialization/template instantiation) problems with factory pattern
trying to force static object initialization
EDIT: There is a duplicate of this but I'll leave this up as I personally had trouble finding it. In addition here's the answer that helped me:
https://stackoverflow.com/a/2852234/673730
Assume the following class:
template<class X>
struct A
{
static bool x;
static bool foo()
{
cout << "here";
return true;
}
};
template<class X>
bool A<X>::x = A<X>::foo();
I would have assumed that when I specialize A
, the static field x
would get initialized. However, the following:
A<int> a;
//no output
doesn't result in a call to foo
. If I try to access the member, the behavior is as expected:
A<int> a;
bool b = a.x;
//output: here
EDIT: How can I make sure A::x
is initialized without accessing it?