C++模板常用使用方法介紹

來源:互聯網
上載者:User

http://developer.51cto.com/art/201002/182202.htm

C++程式設計語言中的模板應用在一定程度上大大提高了程式開發的效率。我們在這篇文章中為大家詳細講解一下有關C++模板的基本概念,希望初學者們可以通過本文介紹的內容充分掌握這方面的知識。

前段時間重新學習C++,主要看C++編程思想和C++設計新思維。對模版的使用有了更進一層的瞭解,特總結如下:

下面列出了C++模板的常用情況:

1. C++模板類靜態成員

  1. template < typename T> struct testClass   
  2. {   
  3. static int _data;   
  4. };   
  5. template< > int testClass< char>::_data = 1;   
  6. template< > int testClass< long>::_data = 2;   
  7. int main( void ) {   
  8. cout < <  boolalpha < <  (1==testClass< char>::_data) < <  endl;   
  9. cout < <  boolalpha < <  (2==testClass< long>::_data) < <  endl;   
  10. }  

2. C++模板類偏特化

  1. template < class I, class O> struct testClass   
  2. {   
  3. testClass() { cout < <  "I, O" < <  endl; }   
  4. };   
  5. template < class T> struct testClass< T*, T*>   
  6. {   
  7. testClass() { cout < <  "T*, T*" < <  endl; }   
  8. };   
  9. template < class T> struct testClass< const T*, T*>   
  10. {   
  11. testClass() { cout < <  "const T*, T*" < <  endl; }   
  12. };   
  13. int main( void )   
  14. {   
  15. testClass< int, char> obj1;   
  16. testClass< int*, int*> obj2;   
  17. testClass< const int*, int*> obj3;   

3.類模版+函數模版

  1. template < class T> struct testClass   
  2. {   
  3. void swap( testClass< T>& ) { cout < <  "swap()" < <  endl; }   
  4. };   
  5. template < class T> inline void swap( testClass< T>& x, 
    testClass< T>& y )   
  6. {   
  7. x.swap( y );   
  8. }   
  9. int main( void )  
  10. {   
  11. testClass< int> obj1;   
  12. testClass< int> obj2;   
  13. swap( obj1, obj2 );   

4. 類成員函數模板

  1. struct testClass  
  2. {   
  3. template < class T> void mfun( const T& t )  
  4. {   
  5. cout < <  t < <  endl;   
  6. }   
  7. template < class T> operator T()   
  8. {   
  9. return T();   
  10. }   
  11. };   
  12. int main( void )   
  13. {   
  14. testClass obj;   
  15. obj.mfun( 1 );   
  16. int i = obj;   
  17. cout < <  i < <  endl;   

5. 預設C++模板參數推導

  1. template < class T> struct test   
  2. {   
  3. T a;   
  4. };   
  5. template < class I, class O=test< I> > struct testClass   
  6. {   
  7. I b;   
  8. O c;   
  9. };   
  10. void main()  
  11. {  

6. 非類型C++模板參數

  1. template < class T, int n> struct testClass {   
  2. T _t;   
  3. testClass() : _t(n) {   
  4. }   
  5. };   
  6. int main( void ) {   
  7. testClass< int,1> obj1;   
  8. testClass< int,2> obj2;   

7. 空模板參數

  1. template < class T> struct testClass;   
  2. template < class T> bool operator==( const testClass< T>&, 
    const testClass< T>& )   
  3. {   
  4. return false;   
  5. };   
  6. template < class T> struct testClass   
  7. {   
  8. friend bool operator== < >
    ( const testClass&, const testClass& );   
  9. };   
  10. void main()  
  11. {  

8. template template 類

  1. struct Widget1   
  2. {   
  3. template< typename T>   
  4. T foo(){}   
  5. };   
  6. template< template< class T>class X>   
  7. struct Widget2  
  8. {   
  9. };   
  10. void main()  
  11. {  
  12. cout< <  3 < <  '\n';  

以上就是對C++模板的一些常用方法的介紹。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.