[英]template specialization with template parameter
我最近开始学习c ++。 我有时会与模板专业化混淆。 有人可以告诉我以下在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
似乎No(3)处的模板专业化没有隐式模板参数,因为类型名T为int。 但是编译器给出以下错误,
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);
^
恐怕我在问一个愚蠢的问题,但我真的很想知道错误的原因。 我不知道No 1和No3的类型名称Ts是否相同。 请告诉我。 非常感谢你。
(2)是写专业化的方法。
T
(1)和(3)是不是真的一样,你可能会写(非专业化):
template <typename U>
void ClassA<U>::func(void) {}
而在(1)中是T
或部分专业
template <typename T>
void ClassA<std::vector<T>>::func(void) {}
其中(1)中的T1
将为std::vector<T3>
。
代替
template<typename T>
void ClassA<int>::func(void) {}
采用
template<typename T>
void ClassA<T>::func(void) {}
// ^^^ T, not it.
我不确定使用int
是故意的还是疏忽大意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.