what is function template in c++:
Function templates provide a functional behavior that can be
called for different types. In other words, a function template represents a
family of functions. The representation looks a lot like an ordinary function,
except that some elements of the function are left undetermined: These elements
are parameterized. To illustrate, let's look at a simple example.
Defining the Template
The following is a function template that returns the maximum
of two values:
This template definition specifies a family of functions that returns the maximum of two values, which are passed as function parameters a and b. The type of these parameters is left open as template parameter T. As seen in this example, template parameters must be announced with syntax of the following form:
In our example, the list of parameters is typename T.
Note how the less-than and the greater-than symbols are used as brackets; we
refer to these as angle brackets. The keyword
typename introduces a so-called type
parameter. This is by far the most common kind of template parameter in
C++ programs, but other parameters are possible, and we discuss them later in coming chapters.
Here, the type parameter is T. You can use any
identifier as a parameter name, but using T is the convention. The type
parameter represents an arbitrary type that is specified by the caller when the
caller calls the function. You can use any type (fundamental type, class, and so
on) as long as it provides the operations that the template uses. In this case,
type T has to support operator < because a and
b are compared using this operator.
For historical reasons, you can also use class instead
of typename to define a type parameter. The keyword typename
came relatively late in the evolution of the C++ language. Prior to that, the
keyword class was the only way to introduce a type parameter, and this
remains a valid way to do so. Hence, the template max() could be
defined equivalently as follows:
Semantically there is no difference in this context. So, even
if you use class here, any type may be
used for template arguments. However, because this use of class can be
misleading (not only class types can be substituted for T), you should
prefer the use of typename in this context. Note also that unlike class
type declarations, the keyword struct cannot be used in place of
typename when declaring type parameters.
Using the Template
The following program shows how to use the max()
function template:
Inside the program, max() is called three times: once
for two ints, once for two doubles, and once for two
std::strings. Each time, the maximum is computed. As a result, the
program has the following output:
Note that each call of the max() template is qualified
with ::. This is to make sure that our max() template is found
in the global namespace. There is also an std::max() template in the
standard library, which under some circumstances may be called or may lead to
ambiguity. [1]
Normally, templates aren't compiled into single entities that
can handle any type. Instead, different entities are generated from the template
for every type for which the template is used. Thus, max() is
compiled for each of these three types. For example, the first call of
max()
uses the function template with int as template
parameter T. Thus, it has the semantics of calling the following
code:
|
Thursday, January 16, 2014
what is function template in c++
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment