【发布时间】:2011-07-21 08:35:42
【问题描述】:
我有一个带有模板化成员函数的模板化类
template<class T>
class A {
public:
template<class CT>
CT function();
};
现在我想以两种方式专门化模板化成员函数。首先与类具有相同的类型:
template<class T>
template<> // Line gcc gives an error for, see below
T A<T>::function<T>() {
return (T)0.0;
}
bool 类型的第二个:
template<class T>
template<>
bool A<T>::function<bool>() {
return false;
}
这是我尝试测试的方法:
int main() {
A<double> a;
bool b = a.function<bool>();
double d = a.function<double>();
}
现在 gcc 给了我上面标记的行:
error: invalid explicit specialization before ‘>’ token
error: enclosing class templates are not explicitly specialize
所以 gcc 告诉我,如果我想专门化函数,我必须专门化 A,对吗? 我不想那样做,我希望外部类的类型是开放的...
最后的答案是:不可能吗?或者有什么办法?
【问题讨论】:
标签: c++ template-specialization