`
javasogo
  • 浏览: 1778744 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

c++ 模板的优点和缺点

 
阅读更多

Templates in C++ - Pros and Cons

By SergeyChepurin | 29 Oct 2011

C++WindowsIntermediate

Advantages and drawbacks of usingtemplates in C++ projects

LicenceCPOL First Posted29Oct 2011 Views3,549 Bookmarked13 times
4.50 (8 votes)

Introduction

Once upon a time, I decided to collect and organize all the advantages anddrawbacks you may experience when using templates in C++.

Advantages

使你能够定义操作不同类型信息的函数族或是类族。
C++ templates enable you to define a family of functions or classes that canoperate on different types of information.

  • Use templates in situations that result in duplication of the same code for multiple types.
    For example, you can use function templates to create a set of functions that apply the same algorithm to different data types.
    在一些场景可以避免重复代码
  • You can also use class templates to develop a set of typesafe classes.
    使用class templates开发一组类型安全的类。
  • Templates are sometimes a better solution than C macros and void pointers,
    and they are especially useful when working with collections (one of the main uses for templates in MFC) and smart pointers (from MSDN).
    相对于c中的宏和void pointers,模版是一个更好的解决方案
  • A. Stepanov (the creator of STL) notes that some things that seem trivial using templates (such as equality operator, for example) are very difficult to implement with conventional OO techniques such as inheritance and polymorphism.
    有些问题难以使用OO技巧(如继承和多态)来实现,而使用模版会很方便
  • Because their parameters are known at compile time, template classes are more typesafe, and could be preferred over run-time resolved code structures (such as abstract classes). There are some modern techniques that can dramatically reduce code bloat when using templates. Note that these techniques are very complex either.
    template classes更加的类型安全,因其参数类型在编译时都是已知的。
  • Often, themain reason to use templates in combination with STL is that it can drastically reduce development time.
    能够减少开发时间(从而提高生产效率)

Disdvantages (//z 11/9/2011 1:10 PM @is2120)

  • Historically, some compilers exhibited poor support for templates. So, the use of templates could decrease code portability.
    一些编译器对template支持不好
  • Many compilers lack clear instructions when they detect a template definition error. This can increase the effort of developing templates, and has prompted the
    development of concepts for possible inclusion in a future C++ standard.
    编译器给出的有些出错信息比较晦涩。
  • Since the compiler generates additional code for each template type, indiscriminate use of templates can lead to code bloat, resulting in larger executables. For example, used in Adobe products "… GIL (Generic Image Library) implements type generators.
    One of these generators generates all image types that are combinations of given sets of color spaces and channels.
    为每种类型都生成额外的代码,可能导致生成的exe膨胀。
    This code defines any image t to be one of 4×3×2×2 = 48 possible image types. It can have any of the four listed color spaces, any of the three listed channel depths, it can be interleaved or planar and its pixels can be adjacent or non-adjacent in memory.
    The above code generates 48 × 48 = 2304 instantiations. Without any special handling, the code bloat will be out of control."
    See Efficient Run-Time Dispatching in Generic Programming with Minimal Code Bloat, 2004.
  • Because a template by its nature exposes its implementation, injudicious use in large systems can lead to longer build times.
    由于template需要输出其实现,因而在大型系统中滥用可能导致构建时间极长
  • It can be difficult to debug code that is developed using templates. Since the compiler replaces the templates, it becomes difficult for the debugger to locate the code at runtime.
    使用templates写的代码难以调试
  • Templates are in the headers, which require a complete rebuild of all project pieces when changes are made.
    templates在头文件中,这样一旦有所变更需要重编译所有相关工程
  • No information hiding. All code is exposed in the header file. No one library can solely contain the code (from Wikipedia).
    没有信息隐藏
  • Though STL itself is a collection of template classes, templates are not used to write conventional libraries.
    The libraries of templates are header-only: the library code is included in and compiled with the user's code.
    Though, this makes installation and usage of the libraries relatively easy.

In The C++ Programming Language (3rd Edition), B.Stroustruppresents over 20 factors to take into account when programming templates. Manyof them have to do with ensuring that your code is reliable for all inputclasses, and maintainable.

B. Stroustrup recognizes these pitfalls:

  • The ease with which unmaintainable "spaghetti code" can be generated
  • Automatically generated source code can become overwhelmingly huge
  • Compile-time processing of templates can be extremely time consuming
  • Debugging is not intuitive for most programmers
  • Context dependencies can be difficult to diagnose and even harder to correct
    (from comments here)

References

History

  • 29.10.2011 - Initial submission of article

License

This article, along with any associated source code and files, is licensedunder The Code ProjectOpen License (CPOL)

About the Author
SergeyChepurin

//z 11/9/2011 1:10 PM @is2120
分享到:
评论

相关推荐

    C++高精度模板(加减乘除模)

    C++竞赛高精度模板 优点:包含四则运算 抛弃传统数组,使用vector 缺点:乘法没有使用FFT算法,复杂度O(N^2) 在某些编辑器上可能无法使用资源中定义的常量,需要手动创建(CE的话可能是这个问题) 没有运算符...

    数据库模板库 OTL

    OTL 是 Oracle, Odbc and DB2-CLI Template Library 的缩写,是一个C++编译中操控关系数据库的模板库,它目前几乎支持所有的当前各种主流数据库,例如Oracle, MS SQL Server, Sybase, Informix, MySQL, DB2, ...

    CC++ 集成开发环境(IDE) DEV-C++ 5.11.0 中文免费版.zip

    Dev-C++是NOI、NOIP等比赛的指定工具,缺点是Debug功能弱。 C/C++ 集成开发环境(IDE) DEV-C++ 中文版C/C++ 集成开发环境(IDE) DEV-C++ 中文版 Dev-C++使用MingW64/TDM-GCC编译器,遵循C++ 11标准,同时兼容C++98...

    首款C C++级别、基于Rust开发的后台管理系统,集成acitx-web高性能、Django模板、前端等各项优点.zip

    这里不对以上框架做优缺点比较,因为自己涉猎也比较浅,所以不知道,则不评价,仅仅是因为自己会简单使用Actix,所以这里对Rust中使用Actix做简单分享。 环境搭建 首先,需要引入相关依赖 这里指定serde的版本和...

    NDK14_C++基础:友元,模板方法,模板类,类型转换

    一 友元 类似 java 中享元模式:实例的共享(ImageLoader Lru 算法 存储图片) 友元:类或函数的共享 ...优点:提高效率、表达清晰 缺点:破坏封装性 特征: 友元函数不能被派生类继承 友元函数不能使用this

    毕业设计(论文)-基于C++的网上考试系统的设计与实现模板

    考试的需要,这种检测方式存在着许多缺点,如:效率低,适用面窄,保密性差,另外 时间一长,由于纸的质量保护和数量管理,这对于查找、更新和维护都带来了不少的困 难。相对传统考试而言,基于 c/s 模式考试系统有许多的...

    C++标准库介绍.pdf

    Windows 时必须编写代码同时它还提供了所有般 C 编程优点例如继承和封装MFC 编写在各个版本 Windows操作系统上是可移植例如在Windows 3.1下编写代码可以很容易地移植到 Windows NT 或 Windows 95 上但是在最近发展...

    Google C++ 编码规范

    C/C++ 函数参数分为输入参数和输出参数两种,有时输入参数也会输出(译者注:值被修改时)。输入参数一般传值或常数引用(const references),输出参数或输入/输出参数为非常数指针(non-const pointers)。对参数...

    常见抗锯齿:SSAA、MSAA、CSAA、HRAA、CFAA、MLAA、FXAA、FXAA、MFAA

    具体是MSAA只对Z缓存(Z-Buffer)和模板缓存(StencilBuffer)中的数据进行超级采样抗锯齿的处理。可以简单理解为只对多边形的边缘进行抗锯齿处理。相比SSAA对画面中所有数据进行处理,MSAA对资源的消耗需求大大减弱...

    OTL+代码+实例+帮助

    OTL 是 Oracle, Odbc and DB2-CLI Template Library 的缩写,是一个C++操控关系数据库的模板库,最新版本4.0.104,参见http://otl.sourceforge.net/ 优点:a. 跨平台 b. 运行效率高,与C语言直接调用API相当 c. ...

    基于遗传算法的排课系统,含论文(排课 遗传算法 设计模式 智能指针 报表)

    优点:1.采用了智能指针技术(没有使用boost库的引用计数型智能指针,而是参考C++沉思录自己实现的),提高了效率,有利于防止内存泄露。个人也建议在看boost库前先看看C++沉思录,大师的经典,对以后很多技术的实现...

    poppler-agg:带有 agg 后端的 poppler 克隆

    AGG 大量使用 C++ 模板,这是一个主要的优点,也是一个潜在的缺点。 一方面,底层色彩空间和色彩存储模型的所有方面都可以通过模板参数进行操作,从而可以为专用硬件(如对自定义色彩空间和位具有非常特殊需求的...

    FSM:适用于游戏的状态机

    #从《游戏人工智能编程》学到的状态机 采用状态模式,与传统不同的是,所有状态都是单例,优点是频繁切换状态时不用重复申请内存,非常...hpp文件包含了类的声明和定义,写模板类的时候比较方便,适合用来编写开源库。

    MFC Windows程序设计(第2版修订版)--源代码

     《mfc windows程序设计(第2版)》是对其极为经典的第1版的全面更新,本书不仅扩展了已被认为是权威的关于microsoft用于windows api的功能强大的c++类库的阐述,还新增了有关com、ole和activex的内容。本书的作者...

    MFC Windows程序设计(第2版修订版)--详细书签版2卷

     《mfc windows程序设计(第2版)》是对其极为经典的第1版的全面更新,本书不仅扩展了已被认为是权威的关于microsoft用于windows api的功能强大的c++类库的阐述,还新增了有关com、ole和activex的内容。本书的作者...

    MFC Windows程序设计(第2版修订版)--详细书签版1卷

     《mfc windows程序设计(第2版)》是对其极为经典的第1版的全面更新,本书不仅扩展了已被认为是权威的关于microsoft用于windows api的功能强大的c++类库的阐述,还新增了有关com、ole和activex的内容。本书的作者...

    CinderBlock-Deferred-Renderer:使用延迟渲染引擎来渲染灯光和 SSAO 的 Cinder 应用程序。 还有点光源阴影贴图

    包括“高级”模板 包括“基本”模板 使用这个引擎的项目 “座” 《太空人牵线木偶》 延迟渲染 + 阴影贴图使用大量的 VRAM(其缺点之一); 但它的优点是可以使大量动态点光源成为可能。 获得更好帧率的提示: 调整...

    tinyasync:使用C ++ 20协程的微型异步网络库

    优点: 微小的。 目前只有5000多行。 这是仅标头的库。 专注。 它仅实现了异步IO库的核心功能。 简单的。 扁平的名称空间/类结构,简单的实现,避免使用过于花哨的模板技术。 便携的。 它支持Windows和Linux(epoll...

    操作系统(内存管理)

    在大部分系统语言中,比如 C 和 C++,您必须进行内存管理。本文将介绍手工的、半手工的以及自动的内存管理实践的基本概念。 追溯到在 Apple II 上进行汇编语言编程的时代,那时内存管理还不是个大问题。您实际上在...

Global site tag (gtag.js) - Google Analytics