且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C++模版全掌握(实例)

更新时间:2022-09-13 20:34:43

前段时间重新学习C++,主要看C++编程思想和C++设计新思维。对模版的使用有了更进一层的了解,特总结如下:

下面列出了模版的常用情况:
C++模版全掌握(实例)//1. 模板类静态成员
C++模版全掌握(实例)
template <typename T> struct testClass 
C++模版全掌握(实例)
C++模版全掌握(实例)    static int _data; 
C++模版全掌握(实例)}

C++模版全掌握(实例)template<> int testClass<char>::_data = 1; 
C++模版全掌握(实例)template<> int testClass<long>::_data = 2; 
C++模版全掌握(实例)int main( void ) 
C++模版全掌握(实例)    cout << boolalpha << (1==testClass<char>::_data) << endl; 
C++模版全掌握(实例)    cout << boolalpha << (2==testClass<long>::_data) << endl; 
C++模版全掌握(实例)}
 
C++模版全掌握(实例)
C++模版全掌握(实例)//2. 模板类偏特化 
C++模版全掌握(实例)
template <class I, class O> struct testClass 
C++模版全掌握(实例)
C++模版全掌握(实例)    testClass() { cout << "I, O" << endl; } 
C++模版全掌握(实例)}

C++模版全掌握(实例)template <class T> struct testClass<T*, T*> 
C++模版全掌握(实例)
C++模版全掌握(实例)    testClass() { cout << "T*, T*" << endl; } 
C++模版全掌握(实例)}

C++模版全掌握(实例)template <class T> struct testClass<const T*, T*> 
C++模版全掌握(实例)
C++模版全掌握(实例)    testClass() { cout << "const T*, T*" << endl; } 
C++模版全掌握(实例)}

C++模版全掌握(实例)int main( void ) 
C++模版全掌握(实例)
C++模版全掌握(实例)    testClass<intchar> obj1; 
C++模版全掌握(实例)    testClass<int*, int*> obj2; 
C++模版全掌握(实例)    testClass<const int*, int*> obj3; 
C++模版全掌握(实例)}
 
C++模版全掌握(实例)
C++模版全掌握(实例)//3.类模版+函数模版
C++模版全掌握(实例)template <class T> struct testClass 
C++模版全掌握(实例)
C++模版全掌握(实例)    void swap( testClass<T>& ) { cout << "swap()" << endl; } 
C++模版全掌握(实例)}

C++模版全掌握(实例)template <class T> inline void swap( testClass<T>& x, testClass<T>& y ) 
C++模版全掌握(实例)
C++模版全掌握(实例)    x.swap( y ); 
C++模版全掌握(实例)}
 
C++模版全掌握(实例)int main( void )
C++模版全掌握(实例)
C++模版全掌握(实例)    testClass<int> obj1; 
C++模版全掌握(实例)    testClass<int> obj2; 
C++模版全掌握(实例)    swap( obj1, obj2 ); 
C++模版全掌握(实例)}
 
C++模版全掌握(实例)
C++模版全掌握(实例)
C++模版全掌握(实例)//4. 类成员函数模板 
C++模版全掌握(实例)
struct testClass
C++模版全掌握(实例)
C++模版全掌握(实例)    template <class T> void mfun( const T& t )
C++模版全掌握(实例)    
C++模版全掌握(实例)        cout << t << endl; 
C++模版全掌握(实例)    }
 
C++模版全掌握(实例)    template <class T> operator T() 
C++模版全掌握(实例)    
C++模版全掌握(实例)        return T(); 
C++模版全掌握(实例)    }
 
C++模版全掌握(实例)}

C++模版全掌握(实例)int main( void ) 
C++模版全掌握(实例)
C++模版全掌握(实例)    testClass obj; 
C++模版全掌握(实例)    obj.mfun( 1 ); 
C++模版全掌握(实例)    int i = obj; 
C++模版全掌握(实例)    cout << i << endl; 
C++模版全掌握(实例)}
 
C++模版全掌握(实例)
C++模版全掌握(实例)//5. 缺省模板参数推导 
C++模版全掌握(实例)
template <class T> struct test 
C++模版全掌握(实例)
C++模版全掌握(实例)    T a; 
C++模版全掌握(实例)}

C++模版全掌握(实例)template <class I, class O=test<I> > struct testClass 
C++模版全掌握(实例)
C++模版全掌握(实例)    I b; 
C++模版全掌握(实例)    O c; 
C++模版全掌握(实例)}

C++模版全掌握(实例)
C++模版全掌握(实例)void main()
C++模版全掌握(实例){
C++模版全掌握(实例)}

C++模版全掌握(实例)
C++模版全掌握(实例)
C++模版全掌握(实例)//6. 非类型模板参数 
C++模版全掌握(实例)
template <class T, int n> struct testClass 
C++模版全掌握(实例)    T _t; 
C++模版全掌握(实例)    testClass() : _t(n) 
C++模版全掌握(实例)    }
 
C++模版全掌握(实例)}

C++模版全掌握(实例)int main( void ) 
C++模版全掌握(实例)    testClass<int,1> obj1; 
C++模版全掌握(实例)    testClass<int,2> obj2; 
C++模版全掌握(实例)}
 
C++模版全掌握(实例)
C++模版全掌握(实例)
C++模版全掌握(实例)//7. 空模板参数 
C++模版全掌握(实例)
template <class T> struct testClass; 
C++模版全掌握(实例)template <class T> bool operator==( const testClass<T>&, const testClass<T>& ) 
C++模版全掌握(实例)
C++模版全掌握(实例)    return false
C++模版全掌握(实例)}

C++模版全掌握(实例)template <class T> struct testClass 
C++模版全掌握(实例)
C++模版全掌握(实例)    friend bool operator== <>( const testClass&, const testClass& ); 
C++模版全掌握(实例)}

C++模版全掌握(实例)void main()
C++模版全掌握(实例){
C++模版全掌握(实例)}

C++模版全掌握(实例)
C++模版全掌握(实例)//8. template template 类
C++模版全掌握(实例)
struct Widget1 
C++模版全掌握(实例)
C++模版全掌握(实例)template<typename T> 
C++模版全掌握(实例)    T foo(){} 
C++模版全掌握(实例)}

C++模版全掌握(实例)
C++模版全掌握(实例)template<template<class T>class X> 
C++模版全掌握(实例)struct Widget2
C++模版全掌握(实例)
C++模版全掌握(实例)}

C++模版全掌握(实例)void main()
C++模版全掌握(实例){
C++模版全掌握(实例)    cout<< 3 << '\n';
C++模版全掌握(实例)}

C++模版全掌握(实例)
C++模版全掌握(实例)
C++模版全掌握(实例)

//参考:http://www.cnblogs.com/dayouluo/archive/2005/05/14/155092.html

特别注意:类,全局函数,类的成员函数都可以特化,但是只有类可以半特化,全局函数和类的成员函数不可以半特化。
C++模版全掌握(实例)//-------------------------------------------
C++模版全掌握(实例)
//1 类的特化和类成员函数的特化
C++模版全掌握(实例)
template<typename T>
C++模版全掌握(实例)class Widget1
C++模版全掌握(实例){
C++模版全掌握(实例)public:
C++模版全掌握(实例)    void Fun1()
C++模版全掌握(实例)    {
C++模版全掌握(实例)        //generic implementation
C++模版全掌握(实例)
    }

C++模版全掌握(实例)    
C++模版全掌握(实例)}
;
C++模版全掌握(实例)
C++模版全掌握(实例)template<>
C++模版全掌握(实例)class Widget1<int>
C++模版全掌握(实例){
C++模版全掌握(实例)public:
C++模版全掌握(实例)    void Fun1()
C++模版全掌握(实例)    {
C++模版全掌握(实例)    }

C++模版全掌握(实例)}
;
C++模版全掌握(实例)template<> 
C++模版全掌握(实例)void Widget1<char>::Fun1()
C++模版全掌握(实例){
C++模版全掌握(实例)    //specialization
C++模版全掌握(实例)
}

C++模版全掌握(实例)
C++模版全掌握(实例)void main()
C++模版全掌握(实例)
C++模版全掌握(实例)
C++模版全掌握(实例)  Widget1<char> w;
C++模版全掌握(实例)  w.Fun1();
C++模版全掌握(实例)  Widget1<int> w2;
C++模版全掌握(实例)  w2.Fun1();
C++模版全掌握(实例)  
C++模版全掌握(实例)}

C++模版全掌握(实例)//-------------------------------------------
C++模版全掌握(实例)
//2 全局函数的特化和重载
C++模版全掌握(实例)
template<typename T1, typename T2>
C++模版全掌握(实例)T1 Fun2(T2)
C++模版全掌握(实例){
C++模版全掌握(实例)}

C++模版全掌握(实例)
C++模版全掌握(实例)//下面2个应该是属于重载
C++模版全掌握(实例)
template<typename T2>
C++模版全掌握(实例)char Fun2(T2)
C++模版全掌握(实例){
C++模版全掌握(实例)    char c;
C++模版全掌握(实例)    return c;
C++模版全掌握(实例)}

C++模版全掌握(实例)
C++模版全掌握(实例)template<typename T1>
C++模版全掌握(实例)T1 Fun2(char)
C++模版全掌握(实例){
C++模版全掌握(实例)}

C++模版全掌握(实例)
C++模版全掌握(实例)//全局函数的特化
C++模版全掌握(实例)
template<>
C++模版全掌握(实例)char Fun2<char,int>(int)
C++模版全掌握(实例){
C++模版全掌握(实例)    char c;
C++模版全掌握(实例)    return c;
C++模版全掌握(实例)}

C++模版全掌握(实例)int main()
C++模版全掌握(实例){
C++模版全掌握(实例)}

C++模版全掌握(实例)//-------------------------------------------
C++模版全掌握(实例)
//3 全局函数不能半特化,以下编译失败
C++模版全掌握(实例)
template <typename T1,typename T2> //原型1
C++模版全掌握(实例)
void Test(T1,T2)
C++模版全掌握(实例){
C++模版全掌握(实例)}

C++模版全掌握(实例)
C++模版全掌握(实例)template <typename T1>
C++模版全掌握(实例)void Test<T1,T1>(T1,T1)
C++模版全掌握(实例){
C++模版全掌握(实例)}

C++模版全掌握(实例)
C++模版全掌握(实例)template<typename T1, typename T2> //原型2
C++模版全掌握(实例)
T1 Fun2(T2)
C++模版全掌握(实例){
C++模版全掌握(实例)}

C++模版全掌握(实例)//
C++模版全掌握(实例)
template<typename T2>
C++模版全掌握(实例)int Fun2<int,T2>(T2)
C++模版全掌握(实例){
C++模版全掌握(实例)}

C++模版全掌握(实例)template<typename T1>
C++模版全掌握(实例)T1 Fun2<T1,int>(int)
C++模版全掌握(实例){
C++模版全掌握(实例)}

C++模版全掌握(实例)template<typename T>
C++模版全掌握(实例)T Fun2<T,T>(T)
C++模版全掌握(实例){
C++模版全掌握(实例)}

C++模版全掌握(实例)int main()
C++模版全掌握(实例){
C++模版全掌握(实例)}

C++模版全掌握(实例)
C++模版全掌握(实例)
C++模版全掌握(实例)////-------------------------------------------
C++模版全掌握(实例)
////4 类可以特化和半特化,但是特的成员函数像全局函数一样,只能特化,不能半特化,

C++模版全掌握(实例)template<typename T1, typename T2> struct Widget2
C++模版全掌握(实例){
C++模版全掌握(实例)  void Fun2()
C++模版全掌握(实例)  {
C++模版全掌握(实例)      //generic implementation
C++模版全掌握(实例)
  }

C++模版全掌握(实例)}
;
C++模版全掌握(实例)
C++模版全掌握(实例)template<typename T2> 
C++模版全掌握(实例)struct Widget2<char,T2>
C++模版全掌握(实例){
C++模版全掌握(实例)    void Fun2()
C++模版全掌握(实例)    {
C++模版全掌握(实例)    }

C++模版全掌握(实例)}
;
C++模版全掌握(实例)
C++模版全掌握(实例)template<typename T2>
C++模版全掌握(实例)struct widget2
C++模版全掌握(实例){
C++模版全掌握(实例)    void Fun2()
C++模版全掌握(实例)    {
C++模版全掌握(实例)        // class partial specialization
C++模版全掌握(实例)
    }

C++模版全掌握(实例)}
;
C++模版全掌握(实例)
C++模版全掌握(实例)
C++模版全掌握(实例)
C++模版全掌握(实例)//the class member funtion can not be partial specialization
C++模版全掌握(实例)
//以下的成员函数半特化,编译失败
C++模版全掌握(实例)
template<typename T2>
C++模版全掌握(实例)void Widget2<char, T2>::Fun2()
C++模版全掌握(实例){
C++模版全掌握(实例)    //class member function partial specialization
C++模版全掌握(实例)
}

C++模版全掌握(实例)int main()
C++模版全掌握(实例){
C++模版全掌握(实例)}

本文转自feisky博客园博客,原文链接:http://www.cnblogs.com/feisky/archive/2009/11/04/1596203.html,如需转载请自行联系原作者