In following code, I got an compiling error
#include <iostream>
using namespace std;
template <typename T, int I>
class MyClass {
public:
void func1(){
cout<<"default: func1"<<endl;
}
void func2(){
cout<<"default: func2"<<endl;
}
private:
T haha;
};
template <typename T>
void MyClass<T, 1>::func1(){
cout<<"special: func1"<<endl;
};
int main()
{
MyClass<int, 2> intclass;
intclass.func1();
intclass.func2();
MyClass<double, 1> doubleclass;
doubleclass.func1();
doubleclass.func2();
return 0;
}
I got following compiling error:
partialspecifizednontype.cpp:19: error: invalid use of incomplete type 'class MyClass<T, 1>'
partialspecifizednontype.cpp:6: error: declaration of 'class MyClass<T, 1>'
Any specific reason that I can't do this? Is there any workaround to achieve this?
I notice that if my template class don't have first parameter things are OK. (my gcc is 4.2.1)
Following example is OK:
#include <iostream>
using namespace std;
template <int I>
class MyClass {
public:
void func1(){
cout<<"default: func1"<<endl;
}
void func2(){
cout<<"default: func2"<<endl;
}
};
template<>
void MyClass<1>::func1(){
cout<<"special: func1"<<endl;
};
int main()
{
MyClass<2> intclass;
intclass.func1();
intclass.func2();
MyClass<1> doubleclass;
doubleclass.func1();
doubleclass.func2();
return 0;
}