模版套模版
1 模版類中有成員是模版:
#include <iostream>
#include <typeinfo>
using namespace std;

template<class T>
class Outer
{
public:
template<class R>
class Inner
{
public:
void f();
};
};

template<class T>
template <class R>
void Outer<T>::Inner<R>::f()
{
cout << "Outer == " << typeid(T).name() << endl;
cout << "Inner == " << typeid(R).name() << endl;
cout << "Full Inner == " << typeid(*this).name() << endl;
}

int main()
{
Outer<int>::Inner<bool> inner;
inner.f();
} ///:~

//output:
//Outer == int
//Inner == bool
//Full Inner == class Outer<int>::Inner<bool>
//Press any key to continue . . .
2 模版類的參數是模版:
// A print function for standard C++ sequences
#include <iostream>
#include <list>
#include <memory>
#include <vector>
#include <deque>
using namespace std;

template<class T, template<class U, class = allocator<U> >
class Seq>
void printSeq(Seq<T>& seq)
{
for (typename Seq<T>::iterator b = seq.begin();
b != seq.end();)
cout << *b++ << endl;
}

int main()
{
// Process a vector
vector<int> v;
v.push_back(1);
v.push_back(2);
printSeq(v);
// Process a list
list<int> lst;
lst.push_back(3);
lst.push_back(4);
printSeq(lst);

// Process a deque
deque<int> d;
d.push_back(5);
d.push_back(6);
printSeq(d);
} ///:~
注意:typename 通知編譯器被限定的標識符應該為類型,不同與typedef 是定義新的類型。




































2 模版類的參數是模版:




































注意:typename 通知編譯器被限定的標識符應該為類型,不同與typedef 是定義新的類型。
posted on 2008-09-14 18:13 肥仔 閱讀(445) 評論(1) 編輯 收藏 引用 所屬分類: C++ 模板