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

C++编程语言中的模板应用在一定程度上大大提高了程序开发的效率。我们在这篇文章中为大家详细讲解一下有关C++模板的基本概念,希望初学者们可以通过本文介绍的内容充分掌握这方面的知识。

前段时间重新学习C++,主要看C++编程思想和C++设计新思维。对模版的使用有了更进一层的了解,特总结如下:

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

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

 template < typename T> struct testClass
{
static int _data;
};
template< > int testClass< char>::_data = ;
template< > int testClass< long>::_data = ;
int main( void ) {
cout < < boolalpha < < (==testClass< char>::_data) < < endl;
cout < < boolalpha < < (==testClass< long>::_data) < < endl;
}

2. C++模板类偏特化

 template < class I, class O> struct testClass
{
testClass() { cout < < "I, O" < < endl; }
};
template < class T> struct testClass< T*, T*>
{
testClass() { cout < < "T*, T*" < < endl; }
};
template < class T> struct testClass< const T*, T*>
{
testClass() { cout < < "const T*, T*" < < endl; }
};
int main( void )
{
testClass< int, char> obj1;
testClass< int*, int*> obj2;
testClass< const int*, int*> obj3;
}

3.类模版+函数模版

 template < class T> struct testClass
{
void swap( testClass< T>& ) { cout < < "swap()" < < endl; }
};
template < class T> inline void swap( testClass< T>& x,
testClass< T>& y )
{
x.swap( y );
}
int main( void )
{
testClass< int> obj1;
testClass< int> obj2;
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( );
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,> obj1;
testClass< int,> obj2;
}

7. 空模板参数

 template < class T> struct testClass;
template < class T> bool operator==( const testClass< T>&,
const testClass< T>& )
{
return false;
};
template < class T> struct testClass
{
friend bool operator== < >
( const testClass&, const testClass& );
};
void main()
{
}

8. template template 类

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

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

C++模板常用使用方法介绍的更多相关文章

  1. WebBrowser常用属性方法介绍

    WebBrowser 常用属性方法 ■■方法 ==============================  ▲GoBack    相当于IE的"后退"按钮,使你在当前历史列表中后 ...

  2. thinkphp模板常用的方法

    thinkphp模板我是看了3.2的文档,对里面的东西过了一遍,然后在写到需要用到模板的东西的时候就有印象,有的能直接回顾,但是有的就可能只知道有这个东西,但是不知道怎么用,所以就重新查手册,这个的话 ...

  3. Node.js GET/POST对应的url/query-string常用的方法介绍

    <一>,在学node.js--GET/POST请求时,先看模块url和query-string的用法 1. 模块url用法,一般用于解析get请求. parse: [Function: u ...

  4. Python中的常用魔术方法介绍

    1.__init__ 初始化魔术方法 触发时机:初始化对象时触发(不是实例化触发,但是和实例化在一个操作中) 参数:至少有一个self,接收对象 返回值:无 作用:初始化对象的成员 注意:使用该方式初 ...

  5. winFrom 常用控件属性及方法介绍

    目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox(文本框)控件 4.RichTextBox控件 5.NumericUpDown控件 6.Button(按钮)控件 7.Gro ...

  6. C# 常用控件属性及方法介绍

      C#常用控件属性及方法介绍                                               目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox ...

  7. 【转载】C#常用控件属性及方法介绍

    C#常用控件属性及方法介绍                                               目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox(文 ...

  8. SVG DOM常用属性和方法介绍(1)

    12.2  SVG DOM常用属性和方法介绍 将以Adobe SVG Viewer提供的属性和方法为准,因为不同解析器对JavaScript以及相关的属性和方法支持的程度不同,有些方法和属性是某个解析 ...

  9. 浅析VS2010反汇编 VS 反汇编方法及常用汇编指令介绍 VS2015使用技巧 调试-反汇编 查看C语言代码对应的汇编代码

    浅析VS2010反汇编 2015年07月25日 21:53:11 阅读数:4374 第一篇 1. 如何进行反汇编 在调试的环境下,我们可以很方便地通过反汇编窗口查看程序生成的反汇编信息.如下图所示. ...

随机推荐

  1. 论Visual Studio和.NET Framework

    今天在工作的时候听到一席谈话感觉有点不可思议,微软真的是把开发人员惯的有点傻了,微软流水线式的产品让很多开发者认定了"唯一",这当然也说明了微软的成功,不扯太多题外话,今天只是简单 ...

  2. 开放封闭原则(OCP,Open Closed Principle)

    tks:http://www.cnblogs.com/Benjamin/p/3251987.html

  3. angular_routerJS_学习

    //这几天看了angularjs和backbone,大看了解了knockout和emberjs,刚刚上网看到了一个angular的router的demo,现在顺便记下来 <!--- DEMO_I ...

  4. java多线程-Semaphore信号量使用

    介绍 信号量(Semaphore),有时被称为信号灯,是在多线程环境下使用的一种设施, 它负责协调各个线程, 以保证它们能够正确.合理的使用公共资源. 概念 Semaphore分为单值和多值两种,前者 ...

  5. nginx 的中文配置详细解释

    文章转自:http://www.ha97.com/5194.html 更详细的模块参数请参考:http://wiki.nginx.org/Main #定义Nginx运行的用户和用户组 user www ...

  6. Rhino Mock

    mock interfaces, delegates and classes, including those with parameterized constructors. set expecta ...

  7. IntelliJ13+tomcat+jrebel实现热部署(亲测可用)

       网上有很多介绍intellij idea整合jrebel插件实现热部署的文章,但是有的比较复杂,有的不能成功,最后经过各种尝试,实现了整合,亲测可用!步骤说明如下:   一.先下载jrebel安 ...

  8. iOS之单例

    今天在看多线程同步时,突然想到了单例的同步问题.自从dispatch_once出现后,我们创建单例非常简单且安全: static dispatch_once_t pred; static Single ...

  9. jquery------.mouseover()和.mouseout()的高级效果使用

    index.jsp <div style="width:100%;height:40px;background-color:#aaa;position:absolute;"& ...

  10. MyEclipse------executeBatch()使用方法

    executeBatch()方法用于成批地执行SQL语句,但不能执行返回值是ResultSet结果集的SQL语句,而是直接执行stmt.executeBatch(); 辅助方法: addBatch() ...