[英]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.