member template是位于class或class template内部的template。一个使用例子如下,这里的Iter即是member template.
#include <vector>
#include <iostream>
using namespace std;
template<typename T>
class Array
{
public:
Array(): _next(0){}
template<typename Iter> Array(Iter beg, Iter end){
_next = 0;
for(; beg != end; beg++){
_array[_next++] = *beg;
}
}
void show() const{
for(int i = 0; i < _next; i++){
cout << _array[i] << " ";
}
cout << endl;
}
private:
enum {size = 100};
T _array[size];
int _next;
};
int main()
{
int a[5] = {4, 5, 6, 7, 8};
Array<int> at(a, a+5);
at.show();
vector<int> vec(a, a+5);
Array<int> av(vec.begin(), vec.end());
av.show();
}
参考:Bruce Eckel. Thinking In C++ Volume 2: Practical Programming. page242.