1.模板的基本语法
模板通常是用一对尖括号给括起来。
如: < Content >
Content内容可以是:
a)class T / typename T
b)一种数据类型,关联T
其中T 就是代表了一种数据类型。
下面用一个几段代码来演示一下
通过 传递一个int 类型的参数调用
那么当你想计算double类型的时候你就要重新写一个新函数
在这里比较幸运的的是 ostream,基本类型都支持。
完整代码如下
看上去,函数的重载是非常好的。但是由于只有一处改动,造成了代码的冗余。因此,我们提出了模板。
模板分为:
有了模板我们可以通过使用我们想要的类型进行调用。而不是通过复制函数来完成我们的任务
下面讲解一下函数模板,首先把等同于上一个效果的实例代码给出
using namespace std;template<class type>void PrintTwice(type data){cout<<"Twice: " << data * 2 << endl;}int _tmain(int argc, _TCHAR* argv[]){PrintTwice(2);PrintTwice(2.2);return 0;}
结果是一样的,这里使用的代码就非常的简洁了。
template<class TYPE>
这行代码通知编译器这是一个函数模板。符号TYPE
将会通过编译器推断出传值过来的数据类型。TYPE
被称为template type parameter.
因此当我调用
的时候,TYPE将会被替换成 int数据类型,编译器将会把模板函数写成
同理当我们调用
的时候就会使用
以上两个函数都是通过数据类型推导出来的,也就是重载了函数。它们都是通过编译器来完成的。而不是通过你手工的复制并修改代码。
下面再来看看另一个函数:
我们注意到我用typename 替换了class。但是这不是因为我们有了返回类型。其实这两个类型是很相似的。几乎无差别。
我们可以如下调用:
编译器会自动给我生成如下:
注意两点
•In third line of code snipped above, Twice is called twice - the return value/type of first call would be the argument/type of second call. Hence, both calls are of int type.
•If a template function is instantiated for a particular data-type, compiler would re-use the same function' instance, if the function is invoked again for same data-type. It means, irrespective of where in your code you invoke the function template with same
type - in same function, in different function, or anywhere in another source file (of same project/build).
在上边的第三行代码中,Twice函数调用了两次。第一次调用返回值将作为其参数的类型。
如果模板函数被实例化特殊的数据类型,编译器将会重复使用同样的函数实例。
Let's write up a function template that would return the addition of two numbers:
今天翻译到此,明天继续。
更多请访问:http://blog.csdn.net/wallwind
分享到:
相关推荐
Templates are among the most powerful features of C++, but they remain misunderstood and underutilized, even as the C++ language and development community have advanced. In C++ Templates, Second Editi...
C++ Templates The Complete Guide (2nd edition) C++ Templates 第2版 英文版
C++ Templates.pdf C++ Templates.pdf C++ Templates.pdf C++ Templates.pdf C++ Templates.pdf C++ Templates.pdf C++ Templates.pdf
C++ Templates - The Complete Guide, 2nd Edition by David Vandevoorde, Nicolai M. Josuttis, and Douglas Gregor Giving guidance on how to use Templates in Modern C++ For programmers, who just use ...
C++ Templates The Complete Guide(2nd) 英文azw3 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
C++ Templates - The Complete Guide, 2nd Edition by David Vandevoorde, Nicolai M. Josuttis, and Douglas Gregor -Giving guidance on how to use Templates in Modern C++ -For programmers, who just use ...
C++ Templates The Complete Guide(2nd) 英文无水印pdf 第2版 pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有...
c++ templates 全览,c++ templates 全览,c++ templates 全览
C++ Templates(简体中文版) C++ Templates(简体中文版) C++ Templates(简体中文版) C++ Templates(简体中文版) C++ Templates(简体中文版)
C++Templates 中文版 侯捷 C++Templates 中文版 侯捷
C++ Templates 中文 8M版 C++ Templates 中文 8M版 C++ Templates 中文 8M版 C++ Templates 中文 8M版
本书是C++模板编程的完全指南,旨在通过基本概念、常用技巧和应用实例3方面的有用资料,为读者打下C++模板知识的坚实基础。 全书共22章。第1章全面介绍了本书的内容结构和相关情况。第1部分(第2~7章)以教程的风格...
《C++ Templates中文版》陈伟柱,人民邮电出版社,扫描版。
本书是C++模板编程的完全指南,旨在通过基本概念、常用技巧和应用实例3方面的有用资料,为读者打下C++模板知识的坚实基础。 全书共22章。第1章全面介绍了本书的内容结构和相关情况。第1部分(第2~7章)以教程的风格...
C++ Templates 第二版英文版的AZW3格式, 适用于kindle阅读. Templates are among the most powerful features of C++, but they are too often neglected, misunderstood, and misused. C++ Templates: The ...
C++ Template 深入讲解。...C++ Templates: The Complete Guide By David Vandevoorde, Nicolai M. Josuttis Publisher : Addison Wesley Pub Date : November 12, 2002 ISBN : 0-201-73484-2 Pages : 552
《C++ Templates中文版》是C++模板编程的完全指南,旨在通过基本概念、常用技巧和应用实例三方面的有用资料,为读者打下C++模板知识的坚实基础。书中详细讲解C++模板语言的概念,使用C++模板的常用设计技巧,还运用...
c++ 模板技术第二版,现代c++ 必备。值得深入学习,很多c++ 库使用了该技术。
C++Templates_c++templates_C++Templates全览_healthbhr.zip
C++ Templates 英文版 格式:PDF 大小:2.55M