Chapter 14 Templates 0 2018, SEU. All rights reserved. 1
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 1 Templates Chapter 14
OBJECTIVES o To use function templates to conveniently create a group of related (overloaded) functions o To distinguish between function templates and function-template specializations o To use class templates to create a group of related types. To distinguish between class templates and class-template specializations. o To overload function templates To understand the relationships among templates, friends, inheritance and static members 0 2018, SEU. All rights reserved. 2
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 2 OBJECTIVES To use function templates to conveniently create a group of related (overloaded) functions. To distinguish between function templates and function-template specializations. To use class templates to create a group of related types. To distinguish between class templates and class-template specializations. To overload function templates. To understand the relationships among templates, friends, inheritance and static members
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. 3
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 3 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
14.1 Introduction o Motivation 实现支持不同数据类型的取最大值函数 (例:支持int、 double、 Hugelnt等) 实现支持不同数据类型的Aray类(例: 支持int、 double、 NugeNt等) 0 2018, SEU. All rights reserved. 4
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 4 14.1 Introduction Motivation 实现支持不同数据类型的取最大值函数 (例:支持int、double、HugeInt等) 实现支持不同数据类型的Array类(例: 支持int、double、HugeInt等)
14.1 Introduction 模板( template):利用一种完全通用的方法来设 计函数或类而不必预先说明将被使用的每个对象 的类型,利用模板功能可以构造相关的函数或类 的系列,因此模板也可称为参数化的类型 泛型编程( Generic Programming °在C++语言中,模板可分为类模板( class template)和函数模板( (function template) 0 2018, SEU. All rights reserved. 5
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 5 14.1 Introduction 模板(template): 利用一种完全通用的方法来设 计函数或类而不必预先说明将被使用的每个对象 的类型,利用模板功能可以构造相关的函数或类 的系列,因此模板也可称为参数化的类型。—— 泛型编程(Generic Programming) 在 C++ 语 言 中 , 模 板 可 分 为 类 模 板 (class template)和函数模板(function template)
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. 6
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 6 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
14.2 Function Templates review °定义模板: template关键字+尖括号<>括起来的 template parameter list(模板参数列表) °模板参数列表: 参数称为 I formal type parameter(形式类型参数)。 在函数调用时替换为基本数据类型或用户自定义数据 类型。 每个参数都必须以关键词 typename(或cass)起 头,参数和参数之间必须以逗号分隔,如 template typename T, typename V> 0 2018, SEU. All rights reserved. 7
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 7 14.2 Function Templates -- review 定义模板: template关键字+尖括号<>括起来的template parameter list(模板参数列表) 模板参数列表: 参数称为formal type parameter(形式类型参数)。 在函数调用时替换为基本数据类型或用户自定义数据 类型。 每个参数都必须以关键词typename (或class )起 头,参数和参数之间必须以逗号分隔,如 template
14.2 Function Templates revlew template typename T>//or template T maximum(t value 1, T value 2, T value3) T maximumValue= value 1: / assume value l is maximum n/ determine whether value2 is greater than maximumvalue if value2> maximum value) maximumValue value2 // determine whether value3 is greater than maximumvalue if ( value3>maximumValue maximumValue value3 return maximumvalue )//end function template maximum 0 2018, SEU. All rights reserved. 8
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 8 14.2 Function Templates -- review template // or template T maximum( T value1, T value2, T value3 ) { T maximumValue = value1; // assume value1 is maximum // determine whether value2 is greater than maximumValue if ( value2 > maximumValue ) maximumValue = value2; // determine whether value3 is greater than maximumValue if ( value3 > maximumValue ) maximumValue = value3; return maximumValue; } // end function template maximum
14.2 Function Templates revlew template∥ or template∥模板声明 T maximun( T value1, T value2, T value3)∥/函数模板定义 T maximumValue= value 1: / assume value l is maximum T指定函数返回值、形参以及局部变量的类型 determIne Whether value2 Is greater than maxImumvalue if value2> maximum value) maximumValue value2 // determine whether value3 is greater than maximumvalue if ( value3>maximumValue maximumValue value3 函数调用形式: return maximumvalue maximum(1, 2, 3); )//end function template maximum maximum(intl, int2, int3); 0 2018, SEU. All rights reserved. 9
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 9 14.2 Function Templates -- review template // or template //模板声明 T maximum( T value1, T value2, T value3 ) //函数模板定义 { T maximumValue = value1; // assume value1 is maximum // determine whether value2 is greater than maximumValue if ( value2 > maximumValue ) maximumValue = value2; // determine whether value3 is greater than maximumValue if ( value3 > maximumValue ) maximumValue = value3; return maximumValue; } // end function template maximum T指定函数返回值、形参以及局部变量的类型 函数调用形式: maximum (1, 2, 3); maximum (int1, int2, int3);
14.2 Function Templates 定义 o template o template< class ElementType r o template< typename BorderType, typename FileType cas; typename(关键字):.函数模板类型参数,任何内 部类型或用户自定义类型。 0 2018, SEU. All rights reserved. 10
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 10 14.2 Function Templates -- 定义 template or template or template class,typename(关键字):函数模板类型参数,任何内 部类型或用户自定义类型