Templates

• An important feature of C++ is templates which provide great flexibility to the language.
• Templates support generic programming, allows developing reusable software components such as function, classes, etc supporting different data types in a single framework.
• For example, function such as sort, swap which support various data types can be developed.
• A template in C++ allows the construction of a family of template functions and class to perform the same operation on different data types.
• The templates declared for function are called function templates and those declared for classes are called class templates.
• They perform appropriate operations depending on the data type of the parameters passed to them. It allows a single template to deal with a generic data type T.

Function Templates

• The limitation of function is they can operate only on a particular data type.
• It can be overcome by defining that function as a function template or generic function.
• A function template specifies how an individual function can be constructed.
• Follow the example program below.(next slide)

Analysis

• Above program of multiple max functions are used to find greater value among two, for different data types. This illustrates the need for function templates. The program consists of 4 max functions.
– int max(int ,int);
– long max(long, long);
– float max(float,float);
– char max(char,char);
• Whose logic of finding greater value is same and differs only in terms of data types.

Using Function Template

• C++ template features enables substitution of a single piece of code for all these overloaded function as follows.
template
T max( T a, T b)
{
return(a>b?a:b);
}
• Such functions are known as function templates.
• When max operation is requested on operands of any data types, the compiler creates a function internally without the user intervention and invokes the same.

Syntax: Function Template

• A function template is prefixed with the keyword template and a list if template type arguments.
• The template-type arguments are called generic data types, since their memory requirement and data representation is not known in the declaration of the function template.
• It is known only at the point of a call to a function template.
template
return_type function_name(arguments)
{
…………..//body of template
…………..
}

Program: Using Template

Function and Function Template

• Function templates are not suitable for handling all data types, so it is necessary to override function templates by using normal function for specific data types.
• If a program has both the function and function template with the same name, first compiler selects the normal function, if it matches with the requested data type, otherwise it creates a function using a function template.

Overloaded Function Templates

• The function templates can also be overloaded with multiple declarations.
• Similar to overloading of normal functions, overloaded function templates must differ either in terms of number of parameters or their type.

Example

Class Templates

• Similar to functions, classes can also be declared to operate on different data types.
• Such classes are called class templates.
• A class template specifies how individual classes can be constructed similar to normal class specification.
• These classes model a generic class which supports similar operations for different data types.
• A generic stack like generic function can be created which can be used for storing data of type integer, floating number, character etc.

0 Comments