【发布时间】:2015-04-30 11:09:00
【问题描述】:
考虑一下这段代码。
template<class T>
class A
{
public:
void f(){..}
void h(){..}
};
template<>
class A<int>
{
public:
void f(){// something different..}
//void h(){..}
};
int main()
{
A<int> obj;
obj.h(); // I want to call A<T>::h(), but compiler erred that there is no h function in A<int>
}
有没有办法打这个电话?或者一些解决方法?
【问题讨论】:
-
编译器是正确的。您可以将
h函数放在别处,例如一个基类。 -
@Eduard Rostomyan 如果它不存在,你将如何称呼它?
-
@VladfromMoscow,我想调用 commot A
:: h(),我不知道怎么做,如果我知道我宁愿问这个问题。 -
@Eduard Rostomyan 按照设计,专业化 A
没有该功能。所以要么你应该重新设计模板,要么不调用函数。
标签: c++ templates template-specialization specialization