模板的问题
来源:百度知道 编辑:UC知道 时间:2024/05/25 00:48:52
有一个模板类,定义为template<typename T,int N>class array{
……
}
如何做一个类型定义,让N=1时类型为T,N>1时类型为template<T,N-1>
用typedef来做,怎么做都行
……
}
如何做一个类型定义,让N=1时类型为T,N>1时类型为template<T,N-1>
用typedef来做,怎么做都行
不大清楚你的意思
是不是可以理解成
N=1 时
template<T,1>
N>1 时
template<T,N-1>
也就是 数组元素个数大小为 N-1 ,当N=1时元素个数为1不为0。
如果这样的话 定义如下
#include <iostream>
using namespace std;
template<class T,int N>
class array{
typedef T DataType;
DataType data[N-1];
};
//特例化
template<class T>
class array<T,1>
{
typedef T DataType;
DataType data[1];
};
int main()
{
cout << sizeof(array<int,10>) << endl;
cout << sizeof(array<int,2>) << endl;
cout << sizeof(array<int,1>) << endl;
return 0;
}