625.2FunctionTemplates: Function Templates- Used to produce overloaded functions that performidentical operations on different types of data.Programmerwrites a single function-template definition. Compilergenerates separateobject-code functions (function-template specializations)based on argument types in calls tothefunctiontemplate- Similar to macros in C, but with full type checking2006PearsonEducation,Inc.Allrightsreserved
© 2006 Pearson Education, Inc. All rights reserved. 6 25.2 Function Templates • Function Templates – Used to produce overloaded functions that perform identical operations on different types of data • Programmer writes a single function-template definition • Compiler generates separate object-code functions (functiontemplate specializations) based on argument types in calls to the function template – Similar to macros in C, but with full type checking
725.2FunctionTemplates(Cont.). Function-template definitions- Preceded by a template header.KeywordtemplatetemplateparameterlistEnclosedinanglebrackets(<and>)-Eachtemplateparameterisprecededbykeywordclassorkeyword typename (both areinterchangeable)- Used to specify types of arguments to,localvariablesinand return type ofthefunction template:Examples template< typename T >- template< class ElementType >- template< typename BorderType, typenameFi1itype >2006PearsonEducation,Inc.Allrightsreserved
© 2006 Pearson Education, Inc. All rights reserved. 7 25.2 Function Templates (Cont.) • Function-template definitions – Preceded by a template header • Keyword template • template parameter list – Enclosed in angle brackets (< and >) – Each template parameter is preceded by keyword classor keyword typename(both are interchangeable) – Used to specify types of arguments to, local variables in and return type of the function template • Examples – template< typename T > – template< class ElementType > – template< typename BorderType, typename Filltype >
//Fig.26.1:fig26_01.cpp82// using template functions.Outline3#include<iostream>using std::cout;4TypetemplateparameterT5using std::endl;specified in template header6Tg26_01.cpp7// function template printArray definition8template< typename T(1 of2)9void printArray( const T *array, int count1011for(int i=o:i<count;i++)12cout<<arrayi』<<";1314cout << endl;153// end function template printArray1617int mainOt1819const int AcouNT =5:// size of array a20const int BcoUNT =7: // size of array b21const int ccouNT = 6; // size of array c2223inta[ACOUNT1=1,2,3,4,57;24doubleb[BcouNT 11.1,2.2,3.3,4.4,5.5,6.6,7.73;25char c[ ccoUNT]="HELLo";// 6thposition for nu112627cout <<"Array a contains:"<< endl;2006PearsonEducationInc.Allrightsreserved
© 2006 Pearson Education, Inc. All rights reserved. 8 Outline fig26_01.cpp (1 of 2) 1 // Fig. 26.1: fig26_01.cpp 2 // Using template functions. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 // function template printArray definition 8 template< typename T > 9 void printArray( const T *array, int count ) 10 { 11 for ( int i = 0; i < count; i++ ) 12 cout << array[ i ] << " "; 13 14 cout << endl; 15 } // end function template printArray 16 17 int main() 18 { 19 const int ACOUNT = 5; // size of array a 20 const int BCOUNT = 7; // size of array b 21 const int CCOUNT = 6; // size of array c 22 23 int a[ ACOUNT ] = { 1, 2, 3, 4, 5 }; 24 double b[ BCOUNT ] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 }; 25 char c[ CCOUNT ] = "HELLO"; // 6th position for null 26 27 cout << "Array a contains:" << endl; Type template parameter T specified in template header
28929//callinteger function-templatespecializationOutline30printArrayCa,AcoUNTHCreates a function-template specialization3132cout <<"Array bcontains:"<< endl;ofprintArraywhereintreplaces T33fig26_01.cpp34l/cal1 double function-template specialization35printArrayCb,BcoUNTHCreates a function-template specialization of36printArray where doublereplaces T37cout <<"Array c contains:"<< endl3839lcall character function-template specialization40printArrayC,ccoUNTO;41return o;Creates afunction-template specialization of42//endmainprintArraywhere char replaces TArray a contains:12345Array b contains:1.1 2.2 3.3 4.4 5.5 6.6 7.7Array c contains:HELLO2006PearsonEducationInc.Allrightsreserved
© 2006 Pearson Education, Inc. All rights reserved. 9 Outline fig26_01.cpp (2 of 2) 28 29 // call integer function-template specialization 30 printArray( a, ACOUNT ); 31 32 cout << "Array b contains:" << endl; 33 34 // call double function-template specialization 35 printArray( b, BCOUNT ); 36 37 cout << "Array c contains:" << endl; 38 39 // call character function-template specialization 40 printArray( c, CCOUNT ); 41 return 0; 42 } // end main Array a contains: 1 2 3 4 5 Array b contains: 1.1 2.2 3.3 4.4 5.5 6.6 7.7 Array c contains: H E L L O Creates a function-template specialization of printArray where int replaces T Creates a function-template specialization of printArray where double replaces T Creates a function-template specialization of printArray where char replaces T
10CommonProgrammingError26.2If a template is invoked with a user-definedtype, and if that template uses functions oroperators (e.g., ==, +, <-) with objects of thatclass type, then those functions and operatorsmust be overloaded for the user-defined type.Forgetting to overload such operators causescompilation errors.2006PearsonEducation,Inc.Allrightsreserved
© 2006 Pearson Education, Inc. All rights reserved. 10 Common Programming Error 26.2 If a template is invoked with a user-defined type, and if that template uses functions or operators (e.g., ==, +, <=) with objects of that class type, then those functions and operators must be overloaded for the user-defined type. Forgetting to overload such operators causes compilation errors