C++模板常用使用方法介绍
转载: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++模板常用使用方法介绍的更多相关文章
- WebBrowser常用属性方法介绍
WebBrowser 常用属性方法 ■■方法 ============================== ▲GoBack 相当于IE的"后退"按钮,使你在当前历史列表中后 ...
- thinkphp模板常用的方法
thinkphp模板我是看了3.2的文档,对里面的东西过了一遍,然后在写到需要用到模板的东西的时候就有印象,有的能直接回顾,但是有的就可能只知道有这个东西,但是不知道怎么用,所以就重新查手册,这个的话 ...
- Node.js GET/POST对应的url/query-string常用的方法介绍
<一>,在学node.js--GET/POST请求时,先看模块url和query-string的用法 1. 模块url用法,一般用于解析get请求. parse: [Function: u ...
- Python中的常用魔术方法介绍
1.__init__ 初始化魔术方法 触发时机:初始化对象时触发(不是实例化触发,但是和实例化在一个操作中) 参数:至少有一个self,接收对象 返回值:无 作用:初始化对象的成员 注意:使用该方式初 ...
- winFrom 常用控件属性及方法介绍
目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox(文本框)控件 4.RichTextBox控件 5.NumericUpDown控件 6.Button(按钮)控件 7.Gro ...
- C# 常用控件属性及方法介绍
C#常用控件属性及方法介绍 目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox ...
- 【转载】C#常用控件属性及方法介绍
C#常用控件属性及方法介绍 目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox(文 ...
- SVG DOM常用属性和方法介绍(1)
12.2 SVG DOM常用属性和方法介绍 将以Adobe SVG Viewer提供的属性和方法为准,因为不同解析器对JavaScript以及相关的属性和方法支持的程度不同,有些方法和属性是某个解析 ...
- 浅析VS2010反汇编 VS 反汇编方法及常用汇编指令介绍 VS2015使用技巧 调试-反汇编 查看C语言代码对应的汇编代码
浅析VS2010反汇编 2015年07月25日 21:53:11 阅读数:4374 第一篇 1. 如何进行反汇编 在调试的环境下,我们可以很方便地通过反汇编窗口查看程序生成的反汇编信息.如下图所示. ...
随机推荐
- 如何使用开源库,吐在VS2013发布之前,顺便介绍下VS2013的新特性"Bootstrap"
刚看到Visual Studio 2013 Preview - ASP.NET, MVC 5, Web API 2新功能搶先看 看了下VS2013带来的"新特性",直觉上看,除了引 ...
- 每天一个linux命令(27):ln 命令
ln 是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接.当我们需要在不同的目录,用到相同的文件时,我们不需要在 每一个需要的目录下都放一个必须相同的文件,我们只 ...
- “耐撕”团队 2016.04.07 站立会议
1. 时间: 20:00--20:15 共计20分钟. 2. 成员: Z 郑蕊 * 组长 (博客:http://www.cnblogs.com/zhengrui0452/), P 濮成林(博客:ht ...
- use case
- 用CSS做长度超过长度显示‘...’,当鼠标放上时显示全部内容
<!DOCTYPE html> <html> <head> <meta name="author" content="Yeeku ...
- 让less编译通过css滤镜
写IE6 hack的时候,发现在less中直接写css滤镜是会报错的,不能编译通过. 解决方法为:用~“”把相关的css代码包裹起来,例如: _top:~"expression(docume ...
- 【转】Oracle集合操作函数:union、intersect、minus
集合操作符专门用于合并多条select 语句的结果,包括:UNION, UNION ALL, INTERSECT, MINUS.当使用集合操作符时,必须确保不同查询的列个数和数据类型匹配. 集合操作符 ...
- Html-Css-li标签增加图片
制作一个需要显示颜色的圆点. <style> .item1 ul{list-style:none} .item1 li{padding-left:20px;background:url(圆 ...
- Oracle 11g 默认用户名和密码
安装ORACLE时,若没有为下列用户重设密码,则其默认密码如下: 用户名 / 密码 登录身份 说明 ...
- visual studio各个版本的差异