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)

相关推荐

  • vue生命周期多少个钩子函数(vue生命周期和钩子函数的区别)

    技术Vue生命周期中的八个钩子函数相机是怎样的Vue生命周期中的八个钩子函数相机是怎样的,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。1、beforeCre

    攻略 2021年12月16日
  • MySQL中MERGE表的优缺点有哪些

    技术MySQL中MERGE表的优缺点有哪些这篇文章将为大家详细讲解有关MySQL中MERGE表的优缺点有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。MERGE表的优点:1.简单管

    攻略 2021年11月1日
  • 如何解决配置文件中的数据库连接串加密了问题

    技术如何解决配置文件中的数据库连接串加密了问题这篇文章主要介绍“如何解决配置文件中的数据库连接串加密了问题”,在日常操作中,相信很多人在如何解决配置文件中的数据库连接串加密了问题问题上存在疑惑,小编查阅了各式资料,整理出

    攻略 2021年10月22日
  • 罗马数字0,罗马数字里的0怎么写啊

    技术罗马数字0,罗马数字里的0怎么写啊罗马数字没0 一.罗马数字 罗马数字是一种现在应用较少的一种的数量表示方式。它的产生晚于中国甲骨文中的数码,更晚于埃及人的十进位数字。但是,它的产生标志着一种古代文明的进步。大约在两

    生活 2021年10月27日
  • 对孩子的期望和鼓励的话,父母对孩子的期望寄语初中生

    技术对孩子的期望和鼓励的话,父母对孩子的期望寄语初中生1家长对初中孩子的期望 1.愿你是一棵树:春天对孩子的期望和鼓励的话,吐一山淡淡的香味;夏天,洒一抹如泉的凉荫;秋天,举一树甜甜的青果;冬天,做一个养精蓄锐的好梦!

    生活 2021年10月21日
  • oracle redo过度诊断是怎样的

    技术oracle redo过度诊断是怎样的oracle redo过度诊断是怎样的,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。一、诊断过度r

    攻略 2021年11月12日