Template (C++)

From Wikipedia, the free encyclopedia
  (Redirected from Template (programming))
Jump to: navigation, search

Templates are a feature of the C++ programming language that allow functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one. This is effectively a Turing-complete language.

Templates are of great utility to programmers in C++, especially when combined with multiple inheritance and operator overloading. The C++ Standard Library provides many useful functions within a framework of connected templates.

Major inspirations for C++ templates were the parametrized modules provided by CLU and the generics provided by Ada.[1]

Technical overview[edit]

There are two kinds of templates: function templates and class templates. (A third kind, variable templates, is expected to feature in C++14.)

Function templates[edit]

A function template behaves like a function except that the template can have arguments of many different types (see example). In other words, a function template represents a family of functions. The format for declaring function templates with type parameters is

template <class    identifier> function_declaration;
template <typename identifier> function_declaration;

Both expressions have exactly the same meaning and behave exactly the same way. The latter form was introduced to avoid confusion because a type parameter does not need to be a class, it may also be a basic type like int or double[citation needed].

For example, the C++ Standard Library contains the function template max(x, y) which returns either x or y, whichever is larger. max() could be defined with the following template:

//This example throws the following error : call of overloaded 'max(double, double)' is ambiguous
template <typename Type>
Type max(Type a, Type b) {
    return a > b ? a : b;
}

This single function definition works with many data types. Although usage of a function template saves space in the source code file (in addition to limiting changes to one function description) versus separate functions written for various datatypes, it does not produce smaller object code than would occur from separate non-templated versions of a function written for different types. For example, if a program uses an int and a double version of the max() function template shown above, the compiler will create an object code version of max() that takes ints and an object code version that takes doubles. The compiler output will be identical to what would have been produced if the source code contained two non-templated versions of max(), one written to handle ints and one written to handle doubles.

#include <iostream>
 
int main()
{
  // This will call max <int> (by argument deduction)
  std::cout << max(3, 7) << std::endl;
  // This will call max<double> (by argument deduction)
  std::cout << max(3.0, 7.0) << std::endl;
  // This type is ambiguous, so explicitly instantiate max<double>
  std::cout << max<double>(3, 7.0) << std::endl;
  return 0;
}

In the first two cases, the template argument Type is automatically deduced by the compiler to be int and double, respectively. In the third case deduction fails because the type of the parameters must in general match the template arguments exactly. This function template can be instantiated with any copy-constructible type for which the expression (y < x) is valid. For user-defined types, this implies that the less-than operator must be overloaded.

Class templates[edit]

A class template provides a specification for generating classes based on parameters. Class templates are generally used to implement containers. A class template is instantiated by passing a given set of types to it as template arguments.[2] The C++ Standard Library contains many class templates, in particular the containers adapted from the Standard Template Library, such as vector.

Template specialization[edit]

When a function or class is instantiated from a template, a specialization of that template is created by the compiler for the set of arguments used, and the specialization is referred to as being a generated specialization.

Explicit template specialization[edit]

Sometimes, the programmer may decide to implement a special version of a function (or class) for a given set of template type arguments which is called an explicit specialization. In this way certain template types can have a specialized implementation that is optimized for the type or more meaningful implementation than the generic implementation.

  • If a class template is specialized by a subset of its parameters it is called partial template specialization (function templates cannot be partially specialized).
  • If all of the parameters are specialized it is a full specialization.

Explicit specialization is used when the behavior of a function or class for particular choices of the template parameters must deviate from the generic behavior: that is, from the code generated by the main template, or templates. For example, the template definition below defines a specific implementation of template "max" for type "bool":

template <>
bool max<bool>(bool a, bool b) {
    return a || b;
}

Variadic templates[edit]

C++11 introduced variadic templates, which can take a variable number of arguments in a manner somewhat similar to variadic functions such as std::printf. Both function templates and class templates can be variadic.

Advantages and disadvantages[edit]

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 function templates 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 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 >.

Generic programming features in other languages[edit]

Initially, the concept of templates was not included in some languages, such as Java and C# 1.0. Java's adoption of generics mimics the behaviour of templates, but is technically different. C# added generics (parameterized types) in .NET 2.0. The generics in Ada predate C++ templates.

Although C++ templates, Java generics, and .NET generics are often considered similar, generics only mimic the basic behavior of C++ templates.[3] Some of the advanced template features utilized by libraries such as Boost and STLSoft, and implementations of the STL itself, for template metaprogramming (explicit or partial specialization, default template arguments, template non-type arguments, template template arguments, ...) are not available with generics.

The D programming language attempts to build on C++ redesigning a better template system.[4] A significant addition is the inclusion of the static if statement, which allows conditional compilation of code based on any information known at compile time. For example:

template factorial(ulong n)
{
    static if( n <= 1 )
        const factorial = 1;
    else
        const factorial = n * factorial!(n-1);
};

D's CTFE (Compile time function execution) feature allows to do the same thing :

ulong factorial(ulong n)
{
    if(n <= 1)
        return 1;
    else
        return n * factorial(n - 1);
}
 
void main()
{
    ulong foo = factorial(4); // known at run-time
    static foo2 = factorial(4); // known at compile-time
}

Also note that the !() delimiters are used rather than the <> delimiters. This prevents ambiguity in the parsing of templates.

Other significant features include typesafe variadic template functions.

// Simple example, assumes all arguments are of the same type.
T[0] max(T...)(T args) {
    static assert(args.length > 1, "Insufficient arguments.");
    // T[0] is the type of the first argument,
    // args[0] is the first argument.
    T[0] max = args[0];
    // Tuple can be iterated over and sliced like an array.
    foreach (arg; args[1 .. $])
        if (arg > max)
            max = arg;
    return max;
}

This function will work for any number of arguments, with the foreach iteration over the tuple of arguments expanded at compile time.

D templates allow a simple form of Constraints too. They can be expressed as an arbitrarily complex predicate that must evaluate at compile time. If it's true the template is a match for the arguments, otherwise the template is ignored during overload matching.[5]

template Foo(int N) if (N & 1)    {...} // A
template Foo(int N) if (!(N & 1)) {...} // B
 
Foo!(3)    // Instantiates A
Foo!(64)   // Instantiates B
 
template Bar(T) if (isFloatingPoint!T) {...}
 
Bar!(3.5) // Instantiates Bar
Bar!(3)   // Fails

Something similar can be done in C++ with Boost enable_if,[6] or with the std::enable_if introduced in C++11.

In C++ templates, the compile-time cases are performed by pattern matching over the template arguments, so the Factorial template's base case is implemented by matching 0 rather than with an inequality test, which is unavailable:

// Induction 
 
template <int N> 
struct Factorial {
  static const int value = N * Factorial<N - 1>::value;
};
 
// Base case via template specialization:
 
template <>
struct Factorial<0> {
  static const int value = 1;
};

With these definitions, one can compute, say 6! at compile time using the expression Factorial<6>::value.

See also[edit]

References[edit]

  1. ^ Stroustrup, Bjarne (2004-09-08). "The C++ Programming Language (Third Edition and Special Edition)". Bjarne Stroustrup's homepage. 
  2. ^ Vandevoorde, Daveed; Josuttis, Nicolai (2002). C++ Templates: The Complete Guide. Addison Wesley. ISBN 0-201-73484-2. 
  3. ^ Differences Between C++ Templates and C# Generics (C# Programming Guide)
  4. ^ (D) Templates Revisited
  5. ^ (D) Template Constraints
  6. ^ Boost enable_if templates

External links[edit]