C++模板使用方法是什么

技术C++模板使用方法是什么本篇文章为大家展示了C++模板使用方法是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。C++编程语言中的模板应用在一定程度上大大提高了程序开发的效

本文向您展示了如何使用C模板。内容简洁易懂,一定会让你眼前一亮。希望通过这篇文章的详细介绍,你能有所收获。

C语言中模板的应用在一定程度上大大提高了程序开发的效率。让我们详细解释一下C模板的基本概念。

前段时间重新学习了C,主要是看C编程思想和C设计新思维。更好地理解模板的使用,总结如下:

下面列出了c模板的常见情况:

1. C++模板类静态成员

templatetypenamestructetstclass { static int _ data;};templateintestclasschar : _ data=1;templateintestclasslong : _ data=2;int main(void){ coutboolalpha(1==testclasschar : _ data)endl;coutboolalpha(2==testclasslong : _ data)endl;}2. C++模板类偏特化

templateclassI,class ostructtestclass { test class(){ cout ' I,O 'endl}};templateclassTstructtestClassT *,T * { TestClass(){ cout T *,T * ' endl}};template classtructetstclassconst *,T * { test class(){ cout ' const *,T * ' endl}};intmain(void){testClassint,charobj1testClassint*,int * obj2testClassconstint *,int * obj3}3.类模版+函数模版

p>

  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. 类成员函数模板

struct testClass  {   template < class T> void mfun( const T& t )  {   cout < <  t < <  endl;   }   template < class T> operator T()   {   return T();   }   };   int main( void )   {   testClass obj;   obj.mfun( 1 );   int i = obj;   cout < <  i < <  endl;   }

5. 缺省C++模板参数推导

template < class T> struct test   {   T a;   };   template < class I, class O=test< I> > struct testClass   {   I b;   O c;   };   void main()  {  }

6. 非类型C++模板参数

template < class T, int n> struct testClass {   T _t;   testClass() : _t(n) {   }   };   int main( void ) {   testClass< int,1> obj1;   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 类

struct Widget1   {   template< typename T>   T foo(){}   };   template< template< class T>class X>   struct Widget2  {   };   void main()  {  cout< <  3 < <  '\n';  }

上述内容就是C++模板使用方法是什么,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/52397.html

(0)

相关推荐

  • 皆为利来,熙熙攘攘皆为利往下句是什么

    技术皆为利来,熙熙攘攘皆为利往下句是什么天下熙熙皆为利来,天下攘攘皆为利往皆为利来。出自:司马迁·西汉《史记 货殖列传》
    译文:天下人为了利益而蜂拥而至,为了利益各奔东西。
    《史记》是西汉著名史学家司马迁撰写的一部纪传体

    生活 2021年10月28日
  • PHP CLI应用的调试原理是什么

    技术PHP CLI应用的调试原理是什么PHP CLI应用的调试原理是什么,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。我们在Eclipse里

    攻略 2021年10月19日
  • golang 相同结构体比较方式比较

    技术golang 相同结构体比较方式比较 golang 相同结构体比较方式比较golang 相同结构体比较方式比较
    使用==
    示例:
    type Student struct {Name stringAg

    礼包 2021年12月24日
  • http协议安全性分析总结(http网络通信报文实时解析)

    技术HTTP协议消息头的示例代码这篇文章主要介绍HTTP协议消息头的示例代码,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!host=gray-scp-getway.9fbank.com
    x-rea

    攻略 2021年12月14日
  • 怎么用Python编写一个简单的游戏

    技术怎么用Python编写一个简单的游戏本篇内容介绍了“怎么用Python编写一个简单的游戏”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅

    攻略 2021年10月20日
  • Java关键字和保留字是什么

    技术Java关键字和保留字是什么这篇文章主要讲解了“Java关键字和保留字是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Java关键字和保留字是什么”吧!  1、访

    攻略 2021年11月4日