14.2 Function Templates 定义 template<类型形参表> 返回值类型函数名形参表) //函数定义体 <类型形参表>可以有一到若干个类型形参,各形参前需 要加上 typename/las关键字,表示传递类型,当有多个 形参时,各形参间用逗号分隔。 <类型形参表>中的每个形参表示一种数据类型。函数模 板“形参表”中至少有一个形参的类型必须用<类型形参 表>中的形参来定义。 函数模板只是说明,不能直接执行,需要特化为模板函数 后才能执行。 0 2018, SEU. All rights reserved. 11
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 11 14.2 Function Templates -- 定义 template <类型形参表> 返回值类型 函数名(形参表) { //函数定义体 } <类型形参表>可以有一到若干个类型形参,各形参前需 要加上typename/class关键字,表示传递类型,当有多个 形参时,各形参间用逗号分隔。 <类型形参表>中的每个形参表示一种数据类型。函数模 板“形参表”中至少有一个形参的类型必须用<类型形参 表>中的形参来定义。 函数模板只是说明,不能直接执行,需要特化为模板函数 后才能执行
/ Fig. 14.1: fig14_01. cpp 2 // Using template functions 3 #include <iostream 4 using std: cout 函数模板的参数T必须在模板头 5 using std: end1 中声明 7// function template printArray definition 8 template< typename t> 9 void printArray c const T *array, int count D 10 for c int i =0: i count; i++) 12 cout < array[ i]<< 13 14 cout < endl 如果是用户自定义类型,必须 15]// end function template printArray 要对<<运算符进行重载 16 17 int main o 18{ 19 const int ACOUNT 5: / size of array a 20 const int BCOUNT 7;// size of array b const int CCoUNT 6:// size array c 22 23inta[ ACOUNT]={1,2,3,4,5}; 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 cout < Array a contains:<< endT 0 2018, SEU. All rights reserved. 12
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 12 1 // Fig. 14.1: fig14_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; 函数模板的参数T必须在模板头 中声明 如果是用户自定义类型,必须 要对<<运算符进行重载
29 // call integer function-template specialization 30 printArray a, ACOUNT ); Creates a function-template specialization of 2 cout < "Array b contains:"<< end l; printArray where int replaces T 33 call double function-template specialization 35 printArray( b, BCOUNT )i t Creates a function-template specialization of 37 cout < Array c contains:"<< end1; printArray where double replaces T 39 / call character function-template specialization 40 printArray( C, CCOUNT )i 41 return o Creates a function-template specialization of 23// end main printArray where char replaces T Array a contaIns: 12345 Array b contains 1.12.23.34.45.56.67.7 Array c contains: 0 2018, SEU. All rights reserved. 13
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 13 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
14.2 Function Templates 函数处理过程 ①在程序中说明了一个函数模板 ②编译系统发现有一个相应的函数调用 例如: printArray(a, ACOUNT) ③编译器寻找和使用最符合函数名和参数类型的函数调用 ④根据实参中的类型来确认是否匹配函数模板中对应的形参, 然后生成一个模板函数(函数模板特化)。(该模板函数的 定义体与函数模板的函数定义体相同) 例如:对于int类型的模板特化: void printArray const d for( inti=OiI< co, void printArray( const int*, int ) cout<< arrayli1< void printArray( const double*, int ) cout<<end: )void printArray( const char*, int ) ⑤编译这个新建的函数 0 2018, SEU. All rights reserved. 14
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 14 14.2 Function Templates -- 函数处理过程 ① 在程序中说明了一个函数模板 ② 编译系统发现有一个相应的函数调用 例如:printArray( a, ACOUNT ); ③ 编译器寻找和使用最符合函数名和参数类型的函数调用 ④ 根据实参中的类型来确认是否匹配函数模板中对应的形参, 然后生成一个模板函数(函数模板特化)。(该模板函数的 定义体与函数模板的函数定义体相同) 例如:对于int类型的模板特化: void printArray( const int *array, int count ) { for ( int i = 0; i < count; i++ ) cout << array[ i ] << " "; cout << endl; } ⑤ 编译这个新建的函数 void printArray( const int *, int ); void printArray( const double *, int ); void printArray( const char *, int );
Topics o 14.1 Introduction o 14.2 Function Templates o 14.3 Overloading Function Templates o 14.4 Class Templates o 14.5 Nontype Parameters and Default Types for Class Templates o 14.6 Notes on Templates and Friends o 14.7 Notes on Templates and static Members 0 2018, SEU. All rights reserved. 15
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 15 Topics 14.1 Introduction 14.2 Function Templates 14.3 Overloading Function Templates 14.4 Class Templates 14.5 Nontype Parameters and Default Types for Class Templates 14.6 Notes on Templates and Friends 14.7 Notes on Templates and static Members