Template (programming) Information & Template (programming) Links at HealthHaven.com
advertise
add site
services
publishers
database
health videos
Bookmark and Share

search wiki for    ?
web dir firms image gallery news pdf wiki shop video 
about
toolbar
stats
live show
health store
more stuff
JOIN/LOGIN
Featured Results:
Vitamin and Mineral Supplements - Template ...
Vitamin and Mineral Supplements - Template...
molecularfitness.com
 Welcome to NESA - New England Sports Academy : Additional Programs and...
Welcome to NESA - New England Sports Academy : Additional Programs and...
nesacademy.com
 Teen Programs,Troubled Teens Program,Troubled Teens Programs California
Teen Programs,Troubled Teens Program,Troubled Teens Programs California
insightpros.com
 Asphalt Green - Adult Programs | Adult Sports & Fitness | Running...
Asphalt Green - Adult Programs | Adult Sports & Fitness | Running...
asphaltgreen.org
 

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.

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.

Contents

[edit] Technical overview

There are two kinds of templates: function templates and class templates.

[edit] Function templates

A function template behaves like a function that can accept arguments of many different types. In other words, a function template represents a family of functions. 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 like this, using the following template:

 #include <iostream>   template <typename T> const T& max(const T& x, const T& y) {   if(y < x)     return x;   return y; }   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; explicitly instantiate max<double>   std::cout << max<double>(3, 7.0) << std::endl;   return 0; } 

In the first two cases, the template argument T 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 exactly match the template arguments. 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.

[edit] Simple class templates

A function template provides a specification for generating template functions, based on some parameters, which all share the same name and are treated as a unit (meaning that, for instance, the programmer just calls max with some arguments, and the appropriate instance of the template materializes).

Similarly, a class template provides a specification for generating classes based on parameters. The following section shows an advanced use of class templates to perform compile-type computation on types. A more common use for class templates, is the definition of polymorphic classes, such as containers.

For example, the C++ standard library has a list container called list, which is a template. The statement list<int> designates or instantiates a linked-list of type int. The statement list<string> designates or instantiates a linked-list of type string. The template has some additional parameters, which take default values if they are not specified. For example, the programmer can write a custom class that provides memory allocation services, and that class can be specified as an argument to the list template, to instantiate a list container that is tightly coupled to this custom allocator (at compile time).

A class template usually defines a set of generic functions that operate on the type specified for each instance of the class (i.e., the parameter between the angle brackets, as shown above). The compiler will generate the appropriate function code at compile-time for the parameter type that appears between the brackets.

[edit] Explicit template specialization

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). However, the programmer may decide to implement a special version of a function (or class) for a given set of template arguments which is called an explicit specialization. If a class template is specialized by a subset of its parameters it is called partial template specialization. If all of the parameters are specialized it is an full specialization. Function templates cannot be partially specialized.

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, consider the max function again. Suppose that the programmer has a class for representing mathematical vectors called vec. This vector class has a member function called norm which returns the length of a vector. The programmer wants to be able to use the max template function over two vec objects, with the semantics that it returns the vector which has the greater norm of the two.

The regular max template does not necessarily work. It wants to compare objects using the greater-than operator:

 template<typename L, typename R> typename promote<L, R>::type max(const L &left, const R &right) {   // requires "::operator > (L, R)" or "L::operator > (R)"   return left > right ? left : right; } 

One obvious way to solve the problem is to make the greater-than operator work for vec objects using their norm, so that this template is then applicable. However, here is how it can be solved with an explicit template specialization for max:

 template<> typename promote<vec, vec>::type max(const vec &left, const vec &right) {   return left.norm() > right.norm() ? left : right; } 

The template specialization provides custom behavior for the template arguments <vec, vec>; the norm member function is called on both vectors to retrieve their lengths, and it is their lengths (assumed to be some scalar numeric type) which are then compared with the greater-than operator.

Also, the previous section Class Templates demonstrates a use of explicit template specialization over class templates to write the base rules for type promotion for two-valued arithmetic operation.

[edit] 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, whereas templates are only expanded inline when the compiler deems it appropriate. When expanded inline, macro functions and template functions have no extraneous run-time overhead. However, template functions will have run-time overhead when they are not expanded inline.

Templates are considered "type-safe", that is, they require type-checking at compile-time. Hence, the compiler can determine at compile-time whether or not 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 is difficult to debug the build, which is developed using templates. Since the compiler replaces the templates, it becomes difficult for the debugger to locate the code at runtime.

Additionally, the use of the "less-than" and "greater-than" signs as delimiters is problematic for tools (such as text editors) which analyse source code syntactically. It is difficult, or maybe 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 integer parameters, each a comparison expression. Alternatively, it could be a declaration of a constructor for class foo taking one parameter, "d", whose type is the parametrised "a < b, c >".

[edit] Generic programming features in other languages

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 behavior 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[1]. 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++ by creating an even more powerful template system. 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); }; 

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

In C++ templates, the compile-time cases are performed by pattern matching over the template arguments, so the Factorial template's base cases are implemented by matching 0 and 1 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 cases via template specialization:   template <> struct Factorial<0> {   static const int value = 1; };   template <> struct Factorial<1> {   static const int value = 1; }; 

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

  1. ^ Differences Between C++ Templates and C# Generics (C# Programming Guide)



Product Results (view all...)

search wiki for    ?
web dir firms image gallery news pdf wiki shop video 



↑ top of page ↑about thumbshots