转自:http://www.cppblog.com/mzty/archive/2007/05/24/24761.html
前段时间重新学习C++,主要看C++编程思想和C++设计新思维。对模版的使用有了更进一层的了解,特总结如下:
下面列出了模版的常用情况:
//1. 模板类静态成员
template <typename T> struct testClass
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
static int _data;
};
template<> int testClass<char>::_data = 1;
template<> int testClass<long>::_data = 2;
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
int main( void )
{
cout << boolalpha << (1==testClass<char>::_data) << endl;
cout << boolalpha << (2==testClass<long>::_data) << endl;
}
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
//2. 模板类偏特化
template <class I, class O> struct testClass
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
testClass()
{ cout << "I, O" << endl; }
};
template <class T> struct testClass<T*, T*>
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
testClass()
{ cout << "T*, T*" << endl; }
};
template <class T> struct testClass<const T*, T*>
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
testClass()
{ cout << "const T*, T*" << endl; }
};
int main( void )
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
testClass<int, char> obj1;
testClass<int*, int*> obj2;
testClass<const int*, int*> obj3;
}
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
//3.类模版+函数模版
template <class T> struct testClass
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
void swap( testClass<T>& )
{ cout << "swap()" << endl; }
};
template <class T> inline void swap( testClass<T>& x, testClass<T>& y )
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
x.swap( y );
}
int main( void )
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
testClass<int> obj1;
testClass<int> obj2;
swap( obj1, obj2 );
}
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
//4. 类成员函数模板
struct testClass
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
template <class T> void mfun( const T& t )
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout << t << endl;
}
template <class T> operator T()
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return T();
}
};
int main( void )
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
testClass obj;
obj.mfun( 1 );
int i = obj;
cout << i << endl;
}
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
//5. 缺省模板参数推导
template <class T> struct test
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
T a;
};
template <class I, class O=test<I> > struct testClass
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
I b;
O c;
};
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
void main()
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
//6. 非类型模板参数
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
template <class T, int n> struct testClass
{
T _t;
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
testClass() : _t(n)
{
}
};
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
int main( void )
{
testClass<int,1> obj1;
testClass<int,2> obj2;
}
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
//7. 空模板参数
template <class T> struct testClass;
template <class T> bool operator==( const testClass<T>&, const testClass<T>& )
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
return false;
};
template <class T> struct testClass
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
friend bool operator== <>( const testClass&, const testClass& );
};
void main()
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
//8. template template 类
struct Widget1
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
template<typename T>
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
T foo()
{}
};
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
template<template<class T>class X>
struct Widget2
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
};
void main()
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
cout<< 3 << '\n';
}
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
//参考:http://www.cnblogs.com/dayouluo/archive/2005/05/14/155092.html
特别注意:类,全局函数,类的成员函数都可以特化,但是只有类可以半特化,全局函数和类的成员函数不可以半特化。
//-------------------------------------------
//1 类的特化和类成员函数的特化
template<typename T>
class Widget1
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
void Fun1()
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//generic implementation
}
};
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
template<>
class Widget1<int>
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
void Fun1()
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
};
template<>
void Widget1<char>::Fun1()
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
//specialization
}
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
void main()
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](//www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
Widget1<char> w;
w.Fun1();
Widget1<int> w2;
w2.Fun1();
}
//-------------------------------------------
//2 全局函数的特化和重载
template<typename T1, typename T2>
T1 Fun2(T2)
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
//下面2个应该是属于重载
template<typename T2>
char Fun2(T2)
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
char c;
return c;
}
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
template<typename T1>
T1 Fun2(char)
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
//全局函数的特化
template<>
char Fun2<char,int>(int)
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
char c;
return c;
}
int main()
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
//-------------------------------------------
//3 全局函数不能半特化,以下编译失败
template <typename T1,typename T2> //原型1
void Test(T1,T2)
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
template <typename T1>
void Test<T1,T1>(T1,T1)
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
template<typename T1, typename T2> //原型2
T1 Fun2(T2)
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
//
template<typename T2>
int Fun2<int,T2>(T2)
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
template<typename T1>
T1 Fun2<T1,int>(int)
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
template<typename T>
T Fun2<T,T>(T)
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
int main()
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/////-------------------------------------------
////4 类可以特化和半特化,但是特的成员函数像全局函数一样,只能特化,不能半特化,
template<typename T1, typename T2> struct Widget2
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
void Fun2()
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//generic implementation
}
};
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
template<typename T2>
struct Widget2<char,T2>
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
void Fun2()
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
};
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
template<typename T2>
struct widget2
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
void Fun2()
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// class partial specialization
}
};
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
//the class member funtion can not be partial specialization
//以下的成员函数半特化,编译失败
template<typename T2>
void Widget2<char, T2>::Fun2()
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
//class member function partial specialization
}
int main()
![](//www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](//www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
参考:C++编程思想2