Starting Out with C++: Early Objects (9th Edition)
Starting Out with C++: Early Objects (9th Edition)
9th Edition
ISBN: 9780134400242
Author: Tony Gaddis, Judy Walters, Godfrey Muganda
Publisher: PEARSON
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 16.2, Problem 16.6CP

Explanation of Solution

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 indentifies 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...

Blurred answer
Students have asked these similar questions
When does the compiler produce code for a function template?
When does the compiler generate function template code?
When does a function template's code get generated by the compiler?

Chapter 16 Solutions

Starting Out with C++: Early Objects (9th Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning