静的メンバーが使用されていない場合、静的メンバー変数はテンプレート クラスで初期化されますか? タイプを登録するために使用します。
template<class T>
class A
{
static bool d;
};
template<class T> bool A<T>::d = [](){regist<A<T>>(); return true;}();
int main()
{
A<int> a;
return 0;
}
私はそれをテストする方法を見つけます。2 以外の 1 を出力します。regist() は呼び出されず、静的メンバーは初期化されません。私のテストはVC110コンパイラーで行われています。また、オンラインでテストします
#include <iostream>
using namespace std;
int i = 1;
template<class T>
void regist()
{
++i;
}
template<class T>
class A
{
static bool d;
};
template<class T> bool A<T>::d = [](){regist<A<T>>(); return true;}();
int main()
{
A<int> a;
cout << i << endl;
return 0;
}