`
javasogo
  • 浏览: 1833964 次
  • 性别: 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++模板函数精讲和仔细分析 C++模板函数是一种强大的工具,可以提供通用的方法来开发可重用的代码。模板函数可以创建参数化的C++类型,实现代码的重用。模板函数可以分为两种类:函数模板和类模板。 函数模板是指...

    c++模板

    通过学习和实践C++模板,开发者可以编写更加灵活和高效的代码,适应不同的数据类型和场景。在实际项目中,合理使用模板能够显著提升代码质量,但同时需要注意避免过度使用导致的复杂性和维护难题。

    c++模板元编程.ex.zip

    因此,在实际应用中,我们需要权衡其优点和缺点,合理地使用模板元编程。 总的来说,C++模板元编程是一种强大的编程技术,它能够帮助我们在编译时生成定制的代码,提高代码的灵活性和效率。然而,这也需要开发者...

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

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

    c++模板元编程.zip

    C++模板元编程是一种在编译时执行计算的高级技术,它是C++语言的一个重要特性,允许程序员...然而,由于其高度抽象和编译时复杂性,模板元编程也可能导致编译错误难以理解和调试,因此在使用时需谨慎权衡其优点和缺点。

    清华大学C++进阶讲义:第9章 模板与群体数据.pdf

    在C++编程语言中,函数模板和类模板是两种非常重要的概念,都是基于模板技术的实现。模板技术可以使得程序更加灵活和可重用。本章将详细介绍函数模板和类模板的定义、使用和应用场景。 函数模板 函数模板是一种...

    C++模板与STL库介绍入门基础.ppt

    模板的优点是克服了 C 语言解决上述问题时用大量不同函数名表示相似功能的坏习惯,克服了宏定义不能进行参数类型检查的弊端,克服了 C++ 函数重载用相同函数名字重写几个函数的繁琐。然而,模板的缺点是调试比较困难...

    数据结构各种算法实现(C++模板)

    在这个上下文中,C++模板可能被用来实现通用的数据结构,使得这些数据结构可以处理不同类型的数据,而无需重复编写代码。这提高了代码的可复用性和灵活性。 总的来说,这个资料集为学习和实践C++中的数据结构和算法...

    c++常用算法模板库.doc

    C++常用算法模板库 C++常用算法模板库是指一系列常用的算法模板库,涵盖了排序算法、数学问题等多个方面。...C++常用算法模板库涵盖了多种排序算法和数学问题,读者可以根据实际情况选择合适的算法。

    C++常用算法模板

    ### C++常用算法模板解析 #### 排序算法 **冒泡排序** 冒泡排序是一种简单的排序算法。它重复地遍历要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。遍历数列的工作是重复进行的,直到没有...

    C++模板与STL库介绍.ppt

    C++模板是C++编程语言中泛型编程的主要工具,它允许程序员创建能够适用于多种数据类型的函数和类。模板的引入极大地提高了代码的复用性和效率,使得程序员无需为每种数据类型分别编写相同功能的代码。 **模板机制的...

    浅谈C++模板元编程

    C++模板元编程的优点包括提高执行效率、高代码重用性和提高代码安全性等。但是,C++模板元编程也存在一些缺点,例如代码可读性差、调试困难、编译时间长等。 C++模板元编程是一种非常有用的编程技术,它可以提高...

    设计模式C++学习之模板方法模式(Template Method)

    在C++中,模板方法模式通常通过继承和虚函数来实现。抽象类(模板类)定义了一个或多个纯虚函数作为模板方法,这些方法在抽象类中只定义了框架,具体的实现由子类提供。例如,我们可以通过以下代码来演示模板方法...

    exceptional c++ 与 more exceptional c++ 英文版

    这两者都有其优点和缺点,书中的讨论有助于读者做出明智的选择。 通过阅读这两本书,C++程序员不仅能提升技术能力,还能培养出对语言内在复杂性的敏锐洞察,从而写出更高效、更可靠、更具可读性的代码。这不仅是...

    C++ 标准模版库STL的应用

    C++ 标准模板库(STL)是C++编程中不可或缺的一部分,它极大地提高了代码的复用性和效率。STL是由一系列模板类和模板函数组成的库,主要包括容器、迭代器、算法和函数对象。 1. **模板机制的介绍** C++中的模板是...

    最速下降法,最速下降法和牛顿法的优缺点,C,C++源码.rar

    C++的模板和STL库可以方便地处理数组和向量操作,而C语言的低级特性则有利于实现高效的内核代码。不过,直接处理大矩阵和向量可能需要额外的库支持,如BLAS和LAPACK。 总的来说,最速下降法和牛顿法是优化问题中两...

    04737C++程序设计精华.pdf

    本资源主要讲述C++程序设计的精华,涵盖了C++语言的基础知识、面向对象编程的特点、函数和函数模板等方面的内容。 认识C++的对象 C++是一种面向对象的编程语言,它支持面向对象编程的三大特点:封装、继承和多态。...

    函数模板与宏定义

    函数模板的主要优点在于类型安全和编译时多态,缺点则是可能导致代码膨胀。 相比之下,宏定义是一种预处理器指令,它在编译前进行文本替换。宏定义使用`#define`关键字,如下所示: ```cpp #define MAX(a, b) ((a)...

Global site tag (gtag.js) - Google Analytics