[英]template specialization with template parameter
I have started learning c++ recently. 我最近开始学习c ++。 And I sometimes confused with template specialization.
我有时会与模板专业化混淆。 Could someone tell me the reason why the following template specialization at No(3) is illegal?
有人可以告诉我以下在No(3)处的模板专业化是非法的原因吗?
template<typename T> // No (1)
class ClassA {
public:
ClassA();
virtual ~ClassA();
void func(void);
};
template<> // No (2)
void ClassA<int>::func(void) {} // Ok legal specialization
template<typename T> // No (3)
void ClassA<int>::func(void) {} // error by compiler
It seems Template specialization at No (3) has no implicit template parameter because typename T is int. 似乎No(3)处的模板专业化没有隐式模板参数,因为类型名T为int。 But compiler give the following error,
但是编译器给出以下错误,
error: prototype for ‘void ClassA<int>::func()’ does not match any in class ‘ClassA<int>’ void ClassA<int>::func(void) {
^
error: candidate is: void ClassA<T>::func() [with T = int] void func(void);
^
I am afraid I am asking a stupid question but I really want to know the reason of error. 恐怕我在问一个愚蠢的问题,但我真的很想知道错误的原因。 And I wonder typename Ts at No 1 and No3 are same or not.
我不知道No 1和No3的类型名称Ts是否相同。 Please tell me them.
请告诉我。 Thank you very much.
非常感谢你。
(2) is the way to write specialization. (2)是写专业化的方法。
T
in (1) and (3) are not really the same, you may write (for non specialization): T
(1)和(3)是不是真的一样,你可能会写(非专业化):
template <typename U>
void ClassA<U>::func(void) {}
whereas it was T
in (1) 而在(1)中是
T
or partial specialize 或部分专业
template <typename T>
void ClassA<std::vector<T>>::func(void) {}
where here the T1
in (1) would be std::vector<T3>
. 其中(1)中的
T1
将为std::vector<T3>
。
Instead of 代替
template<typename T>
void ClassA<int>::func(void) {}
Use 采用
template<typename T>
void ClassA<T>::func(void) {}
// ^^^ T, not it.
I am not sure whether the use of int
was on purpose or it was an oversight. 我不确定使用
int
是故意的还是疏忽大意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.