为什么此代码无法正常工作并给出“未定义的最大值”?
#include <iostream>
using namespace std;
template<typename T>
struct Foo {
static T const max;
};
template<> struct Foo<int> { // Specialization
static int max;
};
template<typename T> T const Foo<T>::max = 22;
template struct Foo<int>;
int main() {
struct Foo<int> ma;
cout << ma.max;
return 0;
}
我定义了静态变量并实例化了模板(我相信显式实例化在这里没有用)。
怎么了?
template<typename T> T const Foo<T>::max = 22;
是一般情况的定义,而不是专门化的情况。
您还必须int Foo<int>::max = 22;
为int专业化定义。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句