Presentation is loading. Please wait.

Presentation is loading. Please wait.

Templates Where the TYPE is generic. Templates for functions Used when the you want to perform the same operation on different data types. The definition.

Similar presentations


Presentation on theme: "Templates Where the TYPE is generic. Templates for functions Used when the you want to perform the same operation on different data types. The definition."— Presentation transcript:

1 Templates Where the TYPE is generic

2 Templates for functions Used when the you want to perform the same operation on different data types. The definition of HOW to perform the operation must be the same for different data types Two new keywords –template –typename

3 Example Suppose you want to swap two integers void swap(int x, int y) { int temp temp = x; x = y; y = temp;} Now suppose you want to swap two characters void swap(char x, char y) {char temp temp = x; x = y; y = temp;}

4 Suppose you want to swap two integers void swap(int x, int y) { int temp temp = x; x = y; y = temp;} Now suppose you want to swap two characters void swap(char x, char y) {char temp temp = x; x = y; y = temp;} A template to swap any two items; the type is generic template void swap(T x, T y) {T temp temp = x; x = y; y = temp;}

5 Example: finding a maximum int max (int v1, int v2, int v3) { int max = v1; if (v2 > max) max = v2; if (v3 > max) max = v3; return max;} template T max (T v1, T v2, T v3) { T max = v1; if (v2 > max) max = v2; if (v3 > max) max = v3; return max;}

6 Instantiating the template function The template itself has no type associated with it. You must tell the compiler what type you want substituted for the T –i.e. you must make an instance of the template with a real data type You instantiate the template function by when you call it with actual data types

7 Calling a template function The call: w = max(x,y,z); If w, x, y, z have been declared as integers, the compiler will make a function where int replaces the T Another call: a = max(b,c,d); If you call again with a, b, c, d which have been declared as floats, the compiler will make another copy, this time substituting float for T

8 Types for function templates The generic type (called the template argument) can be either a built-in type or it can be a user-defined type The generic type(template argument) can be used in three places: –to specify the types of the arguments to the function, –to specify the return type of the function, and –to declare variables within the body of the function definition.

9 Templates are a way to generate code A copy of the function is generated by the compiler for every type for which the function is called So the compiler is actually generating code for you. Note that is not explicitly necessary to tell the compiler the value for T –It can deduce the type from the call

10 A word on terminology template is a keyword and must be used T is user-defined, and could be anything, like replaceIt, or myType typename is a keyword, but is often used interchangeably with the keyword class These are equivalent: template template

11 Templates, the compiler and linker A function template does not do anything by itself, it is only a blueprint the compiler uses to create functions from function calls. The compiler uses a template to generate source code for a function definition, which it then compiles. The role of the linker is to link a single instance of a function into the executable module, even if it is called several times. When the program executes, the existence of a template in the original source code is neither apparent nor relevant.

12 Prototypes You can have a template function prototype template T max (T v1, T v2, T v3); This is often on a single line; white space makes no difference to the compiler

13 Templates and overloading When do you use a template, and when do you overload? Templates and overloading have different purposes –If the definition is the same for different data types, you use a template –If you want to perform similar operations that involve different program logic on different data types you overload

14 For C programmers Macros perform much the same functionality as templates Macros are NOT used much in C++ because: –They do not perform any type checking –This may lead to errors that are very hard to find. Moral: use templates instead of macros!


Download ppt "Templates Where the TYPE is generic. Templates for functions Used when the you want to perform the same operation on different data types. The definition."

Similar presentations


Ads by Google