|
|
10 2.5 |
operate
, but one of them has two parameters of type int
, while the other has them of type double
. The compiler knows which one to call in each case by examining the types passed as arguments when the function is called. If it is called with two int
arguments, it calls to the function that has two int
parameters, and if it is called with two double
s, it calls the one with two double
s.int
version multiplies its arguments, while the double
version divides them. This is generally not a good idea. Two functions with the same name are generally expected to have -at least- a similar behavior, but this example demonstrates that is entirely possible for them not to. Two overloaded functions (i.e., two functions with the same name) have entirely different definitions; they are, for all purposes, different functions, that only happen to have the same name.
|
|
30 2.5 |
sum
is overloaded with different parameter types, but with the exact same body.sum
could be overloaded for a lot of types, and it could make sense for all of them to have the same body. For cases such as this, C++ has the ability to define functions with generic types, known as function templates. Defining a function template follows the same syntax than a regular function, except that it is preceded by the template
keyword and a series of template parameters enclosed in angle-brackets <>:template <template-parameters> function-declaration
class
or typename
keyword followed by an identifier. This identifier can then be used in the function declaration as if it was a regular type. For example, a generic sum
function could be defined as:
|
|
class
or keyword typename
in the template argument list (they are 100% synonyms in template declarations).SomeType
(a generic type within the template parameters enclosed in angle-brackets) allows SomeType
to be used anywhere in the function definition, just as any other type; it can be used as the type for parameters, as return type, or to declare new variables of this type. In all cases, it represents a generic type that will be determined on the moment the template is instantiated.name <template-arguments> (function-arguments)
sum
function template defined above can be called with:
|
|
sum<int>
is just one of the possible instantiations of function template sum
. In this case, by using int
as template argument in the call, the compiler automatically instantiates a version of sum
where each occurrence of SomeType
is replaced by int
, as if it was defined as:
|
|
|
|
11 2.5 |
T
as the template parameter name, instead of SomeType
. It makes no difference, and T
is actually a quite common template parameter name for generic types. sum
twice. The first time with arguments of type int
, and the second one with arguments of type double
. The compiler has instantiated and then called each time the appropriate version of the function.T
is also used to declare a local variable of that (generic) type within sum
:
|
|
a
and b
, and as the type returned by the function.T
is used as a parameter for sum
, the compiler is even able to deduce the data type automatically without having to explicitly specify it within angle brackets. Therefore, instead of explicitly specifying the template arguments with:
|
|
|
|
sum
is called with arguments of different types, the compiler may not be able to deduce the type of T
automatically.
|
|
x and y are equal |
are_equal
:
|
|
|
|
10
) are always of type int
, and floating-point literals without suffix (such as 10.0
) are always of type double
, there is no ambiguity possible, and thus the template arguments can be omitted in the call.class
or typename
, but can also include expressions of a particular type:
|
|
20 30 |
fixed_multiply
function template is of type int
. It just looks like a regular function parameter, and can actually be used just like one.fixed_multiply
, and thus the value of that argument is never passed during runtime: The two calls to fixed_multiply
in main
essentially call two versions of the function: one that always multiplies by two, and one that always multiplies by three. For that same reason, the second template argument needs to be a constant expression (it cannot be passed a variable).Previous: Functions | Index | Next: Name visibility |