鉴于以下情况:
template <typename T0,typename T1,typename T2 , typename T3 , typename T4>
class Tuple
{
private:
T0 v0;
T1 v1;
T2 v2;
T3 v3;
T4 v4;
public:
void f()
{
cout << v0 << "," << v1 << "," << v2 << "," << v3 << "," << v4 << endl;
}
};
我想创建一个只有两个int
-s 的部分类,那么我必须像这样专门化:
class NullType { }; // create an empty class
template <typename T0, typename T1>
class Tuple<T0,T1,NullType,NullType,NullType >
{
private:
T0 v0;
T1 v1;
public:
void func()
{
cout << "i'm a specialization" << endl;
}
};
但是这个实现需要我做:
Tuple<int,int,NullType,NullType,NullType> b;
所以这很丑:)
有没有另一种方法来实现部分专业化而不定义另一个(空)类,所以我可以这样做:Tuple<int,int> b1;
?