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

我们在这篇文章中为大家总结了一些C++模板的常用方法,比如C++模板类静态成员;C++模板类偏特化;类模版+函数模版等。

AD:


    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++模板的一些常用方法的介绍。

    【编辑推荐】

    1. C++存储区域基础概念详解
    2. C++ typedef使用方法总结
    3. C++调用C链接库具体应用技巧讲解
    4. C++类成员相关应用方法介绍
    5. C++循环语句基本概念详解
    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论

    “相关推荐”对你有帮助么?

    • 非常没帮助
    • 没帮助
    • 一般
    • 有帮助
    • 非常有帮助
    提交
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值