C++模板实例掌握
前段时间重新学习C++,主要看C++编程思想和C++设计新思维。对模版的使用有了更进一层的了解,特总结如下:
下面列出了模版的常用情况:
 //1. 模板类静态成员
//1. 模板类静态成员 template <typename T> struct testClass
template <typename T> struct testClass  {
{  static int _data;
    static int _data;  };
};  template<> int testClass<char>::_data = 1;
template<> int testClass<char>::_data = 1;  template<> int testClass<long>::_data = 2;
template<> int testClass<long>::_data = 2;  int main( void ) {
int main( void ) {  cout << boolalpha << (1==testClass<char>::_data) << endl;
    cout << boolalpha << (1==testClass<char>::_data) << endl;  cout << boolalpha << (2==testClass<long>::_data) << endl;
    cout << boolalpha << (2==testClass<long>::_data) << endl;  }
} 
 //2. 模板类偏特化
//2. 模板类偏特化  template <class I, class O> struct testClass
template <class I, class O> struct testClass  {
{  testClass() { cout << "I, O" << endl; }
    testClass() { cout << "I, O" << endl; }  };
};  template <class T> struct testClass<T*, T*>
template <class T> struct testClass<T*, T*>  {
{  testClass() { cout << "T*, T*" << endl; }
    testClass() { cout << "T*, T*" << endl; }  };
};  template <class T> struct testClass<const T*, T*>
template <class T> struct testClass<const T*, T*>  {
{  testClass() { cout << "const T*, T*" << endl; }
    testClass() { cout << "const T*, T*" << endl; }  };
};  int main( void )
int main( void )  {
{  testClass<int, char> obj1;
    testClass<int, char> obj1;  testClass<int*, int*> obj2;
    testClass<int*, int*> obj2;  testClass<const int*, int*> obj3;
    testClass<const int*, int*> obj3;  }
} 
 //3.类模版+函数模版
//3.类模版+函数模版 template <class T> struct testClass
template <class T> struct testClass  {
{  void swap( testClass<T>& ) { cout << "swap()" << endl; }
    void swap( testClass<T>& ) { cout << "swap()" << endl; }  };
};  template <class T> inline void swap( testClass<T>& x, testClass<T>& y )
template <class T> inline void swap( testClass<T>& x, testClass<T>& y )  {
{  x.swap( y );
    x.swap( y );  }
}  int main( void )
int main( void ) {
{  testClass<int> obj1;
    testClass<int> obj1;  testClass<int> obj2;
    testClass<int> obj2;  swap( obj1, obj2 );
    swap( obj1, obj2 );  }
} 

 //4. 类成员函数模板
//4. 类成员函数模板  struct testClass
struct testClass {
{  template <class T> void mfun( const T& t )
    template <class T> void mfun( const T& t ) {
    {  cout << t << endl;
        cout << t << endl;  }
    }  template <class T> operator T()
    template <class T> operator T()  {
    {  return T();
        return T();  }
    }  };
};  int main( void )
int main( void )  {
{  testClass obj;
    testClass obj;  obj.mfun( 1 );
    obj.mfun( 1 );  int i = obj;
    int i = obj;  cout << i << endl;
    cout << i << endl;  }
} 
 //5. 缺省模板参数推导
//5. 缺省模板参数推导  template <class T> struct test
template <class T> struct test  {
{  T a;
    T a;  };
};  template <class I, class O=test<I> > struct testClass
template <class I, class O=test<I> > struct testClass  {
{  I b;
    I b;  O c;
    O c;  };
}; 
 void main()
void main() {
{ }
}

 //6. 非类型模板参数
//6. 非类型模板参数  template <class T, int n> struct testClass {
template <class T, int n> struct testClass {  T _t;
    T _t;  testClass() : _t(n) {
    testClass() : _t(n) {  }
    }  };
};  int main( void ) {
int main( void ) {  testClass<int,1> obj1;
    testClass<int,1> obj1;  testClass<int,2> obj2;
    testClass<int,2> obj2;  }
} 

 //7. 空模板参数
//7. 空模板参数  template <class T> struct testClass;
template <class T> struct testClass;  template <class T> bool operator==( const testClass<T>&, const testClass<T>& )
template <class T> bool operator==( const testClass<T>&, const testClass<T>& )  {
{  return false;
    return false;  };
};  template <class T> struct testClass
template <class T> struct testClass  {
{  friend bool operator== <>( const testClass&, const testClass& );
    friend bool operator== <>( const testClass&, const testClass& );  };
};  void main()
void main() {
{ }
}
 //8. template template 类
//8. template template 类 struct Widget1
struct Widget1  {
{  template<typename T>
template<typename T>  T foo(){}
    T foo(){}  };
}; 
 template<template<class T>class X>
template<template<class T>class X>  struct Widget2
struct Widget2 {
{  };
};  void main()
void main() {
{ cout<< 3 << '\n';
    cout<< 3 << '\n'; }
}


//参考:http://www.cnblogs.com/dayouluo/archive/2005/05/14/155092.html
特别注意:类,全局函数,类的成员函数都可以特化,但是只有类可以半特化,全局函数和类的成员函数不可以半特化。
 //-------------------------------------------
//------------------------------------------- //1 类的特化和类成员函数的特化
//1 类的特化和类成员函数的特化 template<typename T>
template<typename T> class Widget1
class Widget1 {
{ public:
public: void Fun1()
    void Fun1() {
    { //generic implementation
        //generic implementation }
    } 
     };
};
 template<>
template<> class Widget1<int>
class Widget1<int> {
{ public:
public: void Fun1()
    void Fun1() {
    { }
    } };
}; template<>
template<>  void Widget1<char>::Fun1()
void Widget1<char>::Fun1() {
{ //specialization
    //specialization }
}
 void main()
void main() {
{ 
 Widget1<char> w;
  Widget1<char> w; w.Fun1();
  w.Fun1(); Widget1<int> w2;
  Widget1<int> w2; w2.Fun1();
  w2.Fun1(); 
   }
} //-------------------------------------------
//------------------------------------------- //2 全局函数的特化和重载
//2 全局函数的特化和重载 template<typename T1, typename T2>
template<typename T1, typename T2> T1 Fun2(T2)
T1 Fun2(T2) {
{ }
}
 //下面2个应该是属于重载
//下面2个应该是属于重载 template<typename T2>
template<typename T2> char Fun2(T2)
char Fun2(T2) {
{ char c;
    char c; return c;
    return c; }
}
 template<typename T1>
template<typename T1> T1 Fun2(char)
T1 Fun2(char) {
{ }
}
 //全局函数的特化
//全局函数的特化 template<>
template<> char Fun2<char,int>(int)
char Fun2<char,int>(int) {
{ char c;
    char c; return c;
    return c; }
} int main()
int main() {
{ }
} //-------------------------------------------
//------------------------------------------- //3 全局函数不能半特化,以下编译失败
//3 全局函数不能半特化,以下编译失败 template <typename T1,typename T2> //原型1
template <typename T1,typename T2> //原型1 void Test(T1,T2)
void Test(T1,T2) {
{ }
}
 template <typename T1>
template <typename T1> void Test<T1,T1>(T1,T1)
void Test<T1,T1>(T1,T1) {
{ }
}
 template<typename T1, typename T2> //原型2
template<typename T1, typename T2> //原型2 T1 Fun2(T2)
T1 Fun2(T2) {
{ }
} //
// template<typename T2>
template<typename T2> int Fun2<int,T2>(T2)
int Fun2<int,T2>(T2) {
{ }
} template<typename T1>
template<typename T1> T1 Fun2<T1,int>(int)
T1 Fun2<T1,int>(int) {
{ }
} template<typename T>
template<typename T> T Fun2<T,T>(T)
T Fun2<T,T>(T) {
{ }
} int main()
int main() {
{ }
}

 ////-------------------------------------------
////------------------------------------------- ////4 类可以特化和半特化,但是特的成员函数像全局函数一样,只能特化,不能半特化,
////4 类可以特化和半特化,但是特的成员函数像全局函数一样,只能特化,不能半特化, template<typename T1, typename T2> struct Widget2
template<typename T1, typename T2> struct Widget2 {
{ void Fun2()
  void Fun2() {
  { //generic implementation
      //generic implementation }
  } };
};
 template<typename T2>
template<typename T2>  struct Widget2<char,T2>
struct Widget2<char,T2> {
{ void Fun2()
    void Fun2() {
    { }
    } };
};
 template<typename T2>
template<typename T2> struct widget2
struct widget2 {
{ void Fun2()
    void Fun2() {
    { // class partial specialization
        // class partial specialization }
    } };
};


 //the class member funtion can not be partial specialization
//the class member funtion can not be partial specialization //以下的成员函数半特化,编译失败
//以下的成员函数半特化,编译失败 template<typename T2>
template<typename T2> void Widget2<char, T2>::Fun2()
void Widget2<char, T2>::Fun2() {
{ //class member function partial specialization
    //class member function partial specialization }
} int main()
int main() {
{ }
}
参考:C++编程思想2
http://www.cnblogs.com/feisky/archive/2009/11/04/1596203.html
 
微信公众号:
猿人谷 
如果您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】
如果您希望与我交流互动,欢迎关注微信公众号
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
 
                    
                 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号