Почему статические поля шаблона не инициализируются, если не используются? [Дубликат]
Possible Duplicate:
(static initialization/template instantiation) problems with factory pattern
trying to force static object initialization
https://stackoverflow.com/a/2852234/673730
Предположим, следующий класс:
<code>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(); </code>
Я бы предположил, что, когда я специализируюсьA
статическое полеx
будет инициализирован. Тем не менее, следующее:
<code>A<int> a; //no output </code>
не приводит к вызовуfoo
, Если я пытаюсь получить доступ к члену, поведение будет таким, как ожидалось:
<code>A<int> a; bool b = a.x; //output: here </code>
РЕДАКТИРОВАТЬ:How can I make sure A::x
is initialized without accessing it?