如果不使用静态成员,是否在模板类中初始化静态成员变量?我用它来注册类型。
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;
}
我找到了一种方法来测试它。它打印 1 而不是 2。 regist() 不被称为 abd 静态成员未初始化。我的测试是在 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;
}