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
intarguments, it calls to the function that has two
intparameters, and if it is called with two
doubles, it calls the one with two
intversion multiplies its arguments, while the
doubleversion 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.
sumis overloaded with different parameter types, but with the exact same body.
sumcould 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
templatekeyword and a series of template parameters enclosed in angle-brackets <>:
template <template-parameters> function-declaration
typenamekeyword 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
sumfunction could be defined as:
typenamein the template argument list (they are 100% synonyms in template declarations).
SomeType(a generic type within the template parameters enclosed in angle-brackets) allows
SomeTypeto 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)
sumfunction 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
intas template argument in the call, the compiler automatically instantiates a version of
sumwhere each occurrence of
SomeTypeis replaced by
int, as if it was defined as:
Tas the template parameter name, instead of
SomeType. It makes no difference, and
Tis actually a quite common template parameter name for generic types.
sumtwice. 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.
Tis also used to declare a local variable of that (generic) type within
b, and as the type returned by the function.
Tis 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:
sumis called with arguments of different types, the compiler may not be able to deduce the type of
x and y 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.
typename, but can also include expressions of a particular type:
fixed_multiplyfunction 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
mainessentially 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).