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++)