Problem Solving with C++ Plus MyLab Programming with Pearson eText -- Access Card Package (10th Edition)
Problem Solving with C++ Plus MyLab Programming with Pearson eText -- Access Card Package (10th Edition)
10th Edition
ISBN: 9780134710747
Author: Walter Savitch
Publisher: PEARSON
Question
Book Icon
Chapter 17.1, Problem 6STE
Program Plan Intro

Function template:

In C++, a function template is referred as a “generic” function, which can work with different data types.

  • While writing a function template, a programmer should use a “type parameter” to denote a “generic” data type instead of using the actual parameter.
  • The compiler generates the code, when it encounters a function call to a function template. This code will handle the particular data type which is used in the function call.
  • The compiler identifies the argument type and generates the code to work with those types.
  • The generated code is referred as “template function”.

Example:

For example consider the following function template for finding a cube of value:

  template <class T>

  T cube(T x)

  {

  return x * x * x ;

  }

  • A function template must begin with the keyword “template” and it is followed by a pair of angle brackets, which contains one or more “generic” data types.
  • A “generic” type must start with the keyword “class”, followed by an argument name which stands for the data type.
  • The statement “T cube(T x)” is referred as function header, where “T” is a “type parameter”, “cube” is the function name and the variable “x” is declared for the type “T”.

Blurred answer
Students have asked these similar questions
Give an example of how the background process of invoking inline functions is different from that of calling a standard function, and explain how the difference came about.
Sally and Harry are developing a Coin version. Sally specifies that the class of the single parameter to her compareTo function is Object. Harry defines the input to be of type Coin because he is aware that it will always be called on items of that type. Which approach is appropriate for placing Coin items in an OrderedVector?
Suppose that an array is passed as a parameter. How does this differ from the usual use of a value parameter? When an array is passed as a parameter, it is like passed by reference. A new array will be created in the called function and any changes to the new array will pass back to the original array. When an array is passed as a parameter, it is passed by value. So any changes to the parameter do not affect the actual argument. When an array is passed as a parameter, changes to the array affect the actual argument. This is because the parameter is treated as a pointer that points to the first component of the array. This is different from a value parameter (where changes to the parameter do not affect the actual argument). When an array is passed as a value, changes to the array affect the actual argument. This is because the parameter is treated as a pointer that points to the first component of the array. This is different from a parameter (where changes to the parameter do not…
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning