Template (C++) - Advantages and Disadvantages

Advantages and Disadvantages

Some uses of templates, such as the maximum function, were previously fulfilled by function-like preprocessor macros. For example, the following is a C++ maximum macro:

#define maximum(a,b) ((a) < (b) ? (b) : (a))

Both macros and templates are expanded at compile time. Macros are always expanded inline, while templates are only expanded inline when the compiler deems it appropriate. When expanded inline, macro functions and template functions have no extraneous runtime overhead. Template functions with many lines of code will incur runtime overhead when they are not expanded inline, but the reduction in code size may help the code to load from disk more quickly or fit within RAM caches.

Templates are considered type-safe; that is, they require type-checking at compile time. Hence, the compiler can determine at compile time whether the type associated with a template definition can perform all of the functions required by that template definition.

By design, templates can be utilized in very complex problem spaces, whereas macros are substantially more limited.

There are fundamental drawbacks to the use of templates:

  1. Historically, some compilers exhibited poor support for templates. So, the use of templates could decrease code portability.
  2. 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.
  3. Since the compiler generates additional code for each template type, indiscriminate use of templates can lead to code bloat, resulting in larger executables.
  4. Because a template by its nature exposes its implementation, injudicious use in large systems can lead to longer build times.
  5. 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.
  6. Templates of Templates (nesting) are not supported by all compilers, or might have a max nesting level.
  7. Templates are in the headers, which require a complete rebuild of all project pieces when changes are made.
  8. No information hiding. All code is exposed in the header file. No one library can solely contain the code.

Additionally, the use of the "less than" and "greater than" signs as delimiters is problematic for tools (such as text editors) which analyze source code syntactically. It is difficult, or sometimes impossible, for such tools to determine whether a use of these tokens is as comparison operators or template delimiters. For example, this line of code:

foo (a < b, c > d) ;

may be a function call with two parameters, each the result of a comparison expression. Alternatively, it could be a declaration of a constructor for class foo taking a parameter d whose type is the parameterized a < b, c >.

Read more about this topic:  Template (C++)

Famous quotes containing the word advantages:

    Men hear gladly of the power of blood or race. Every body likes to know that his advantages cannot be attributed to air, soil, sea, or to local wealth, as mines and quarries, nor to laws and traditions, nor to fortune, but to superior brain, as it makes the praise more personal to him.
    Ralph Waldo Emerson (1803–1882)