#include <iostream> using namespace std; template <typename T> class testClass{ public: static int _data; }; int testClass<int>::_data = 1; int testClass<char>::_data = 2; int main() { cout << testClass<int>::_data << endl; cout << testClass<char>::_data << endl; testClass<int> obji1, obji2; testClass<char> objc1, objc2; cout << obji1._data << endl; cout << obji2._data << endl; cout << objc1._data << endl; cout << objc2._data << endl; obji1._data = 3; objc2._data = 4; cout << obji1._data << endl; cout << obji2._data << endl; cout << objc1._data << endl; cout << objc2._data << endl; return 0; }
#include <iostream> using namespace std; template <class I, class O> struct testClass { testClass(){cout << "I, O" << endl;} }; template <class T> struct testClass<T*, T*> { testClass(){cout << "T*, T*" << endl;} }; template <class T> struct testClass<const T*, T*> { testClass(){cout << "const T*, T*" << endl;} }; int main() { testClass<int ,char> obj1; testClass<int *, int *> obj2; testClass<const int*, int*> obj3; return 0; }
#include <iostream> using namespace std; class alloc{ }; template <class T, class Alloc = alloc> class vector { public : void swap(vector<T, Alloc>&) { cout << "swap()" << endl;} }; #ifdef _STL_FUNCTION_TMPL_PARTIAL_ORDER template <class T, class Alloc> inline void swap(vector<T, Alloc>& x, vector<T, Alloc>& y) { x.swap(y); } #endif int main() { vector<int> x, y; swap(x, y); return 0; }
#include <iostream> using namespace std; class alloc { }; template <class T, class Alloc = alloc> class vector{ public : typedef T value_type; typedef value_type* iterator; template <class I> void insert(iterator position, I first, I last){ cout << "insert()" << endl; } }; int main() { int ia[5]={0,1,2,3,4}; vector<int> x; vector<int>::iterator ite; x.insert(ite, ia, ia+5); return 0; }
#include <iostream> #include <cstddef> using namespace std; class alloc{ }; template <class T, class Alloc = alloc, size_t BufSiz = 0> class deque { public : deque() { cout << "deque" << endl; } }; template <class T, class Sequence = deque<T> > class stack { public : stack() { cout << "stack" << endl; } private: Sequence c; }; int main() { stack<int> x; return 0; }
原文地址:http://blog.csdn.net/china_zoujinyong/article/details/24700235