[英]Template specialization, specialized class question
我有以下代碼,當我在 A 部分編譯時,function 與模板專業化不匹配。 有誰知道這是怎么回事?
我在#ifdef 代碼中定義了一個專門的 class 。
謝謝
#include <iostream>
using namespace std;
template<class T1, class T2, int I>
class A {
public:
void f0();
};
template<class T1, class T2, int I>
void A<T1, T2, I>::f0()
{
cout << "x" << endl;
}
#if 0 // A
template<>
class A<int, int, 4> {
public:
void f0();
};
#endif
template<>
void A<int, int, 4>::f0()
{
cout << "z" << endl;
}
int main(int argc, char *argv[])
{
A<int, int, 4> a0;
a0.f0();
return 0;
}
#ifdef 編譯時的編譯錯誤:
clang++ -std=c++11 -pedantic -Wall test177.cc && ./a.out
test177.cc:25:22: error: no function template matches function template
specialization 'f0'
void A<int, int, 4>::f0()
您需要編寫沒有“模板<>”的成員 function,如下所示:
template<>
class A<int, int, 4> {
public:
void f0();
};
void A<int, int, 4>::f0()
{
cout << "z" << endl;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.