After C++ experimented with the approaches described later, the C++ Standard settled upon this approach:
For a given function name, you can have a non-template function, a template function, and an explicit specialization
template function.
The prototype and definition for an explicit specialization should be preceded by template <> and should mention
the specialized type by name.
A specialization overrides the regular template, and a non-template function overrides both.
Here's how prototypes for swapping type job structures would look for these three forms:
// non-template function prototype
void Swap(job &, job &);
// template prototype
template <class Any>
void Swap(Any &, Any &);
// explicit specialization for the job type
template <> void Swap<job>(job &, job &);
As mentioned previously, if more than one of these prototypes is present, the compiler chooses the non-template
version over explicit specializations and template versions, and it chooses an explicit specialization over a version
generated from a template.
When the compiler uses the template to generate a function definition for a particular type, the result is termed
an instantiation of the template.
Originally, implicit instantiation was the only way the compiler generated function definitions from templates,
but now C++ allows for explicit instantiation. That means you can instruct the compiler to create a particular
instantiation, for example, Swap<int>(), directly. The syntax is to declare the particular variety you want, using
the <> notation to indicate the type and prefixing the declaration with the keyword template:
template void Swap<int>(int, int); // explicit instantiation
A compiler that implements this feature will, upon seeing this declaration, use the Swap() template to generate
an instantiation using the int type. That is, this declaration means "Use the Swap() template to generate a function
definition for the int type."
Contrast the explicit instantiation with the explicit specialization, which uses one or the other of these equivalent
declarations:
template <> void Swap<int>(int &, int &); // explicit specialization
template <> void Swap(int &, int &); // explicit specialization
The difference is that these declarations mean "Don't use the Swap() template to generate a function definition.
Instead, use a separate, specialized function definition explicitly defined for the int type." These prototypes have
to be coupled with their own function definitions.
It is an error to try to use both an explicit instantiation and an explicit specialization for the same type(s) in
the same programming unit.
Implicit instantiations, explicit instantiations, and explicit specializations collectively are termed specializations.
What they all have in common is that they represent a function definition using specific types rather than one that
is a generic description.
The addition of the explicit instantiation led to the new syntax of using template and template <> prefixes in
declarations to distinguish between the explicit instantiation and the explicit specialization.
Instantiations is just a process to form a function Specializations from a template. Specializations is a result from
an instantiation or from Specializations definitions.
...
template <class Any>
void Swap (Any &, Any &); // template prototype
template <> void Swap<int>(job &, job &); // explicit specialization for job
int main(void)
{
template void Swap<char>(char &, char &); // explicit instantiation for char
short a, b;
...
Swap(a,b); // implicit template instantiation for short
job n, m;
...
Swap(n, m); // use explicit specialization for job
char g, h;
...
Swap(g, h); // use explicit template instantiation for char
...
}
For a given function name, you can have a non-template function, a template function, and an explicit specialization
template function.
The prototype and definition for an explicit specialization should be preceded by template <> and should mention
the specialized type by name.
A specialization overrides the regular template, and a non-template function overrides both.
Here's how prototypes for swapping type job structures would look for these three forms:
// non-template function prototype
void Swap(job &, job &);
// template prototype
template <class Any>
void Swap(Any &, Any &);
// explicit specialization for the job type
template <> void Swap<job>(job &, job &);
As mentioned previously, if more than one of these prototypes is present, the compiler chooses the non-template
version over explicit specializations and template versions, and it chooses an explicit specialization over a version
generated from a template.
When the compiler uses the template to generate a function definition for a particular type, the result is termed
an instantiation of the template.
Originally, implicit instantiation was the only way the compiler generated function definitions from templates,
but now C++ allows for explicit instantiation. That means you can instruct the compiler to create a particular
instantiation, for example, Swap<int>(), directly. The syntax is to declare the particular variety you want, using
the <> notation to indicate the type and prefixing the declaration with the keyword template:
template void Swap<int>(int, int); // explicit instantiation
A compiler that implements this feature will, upon seeing this declaration, use the Swap() template to generate
an instantiation using the int type. That is, this declaration means "Use the Swap() template to generate a function
definition for the int type."
Contrast the explicit instantiation with the explicit specialization, which uses one or the other of these equivalent
declarations:
template <> void Swap<int>(int &, int &); // explicit specialization
template <> void Swap(int &, int &); // explicit specialization
The difference is that these declarations mean "Don't use the Swap() template to generate a function definition.
Instead, use a separate, specialized function definition explicitly defined for the int type." These prototypes have
to be coupled with their own function definitions.
It is an error to try to use both an explicit instantiation and an explicit specialization for the same type(s) in
the same programming unit.
Implicit instantiations, explicit instantiations, and explicit specializations collectively are termed specializations.
What they all have in common is that they represent a function definition using specific types rather than one that
is a generic description.
The addition of the explicit instantiation led to the new syntax of using template and template <> prefixes in
declarations to distinguish between the explicit instantiation and the explicit specialization.
Instantiations is just a process to form a function Specializations from a template. Specializations is a result from
an instantiation or from Specializations definitions.
...
template <class Any>
void Swap (Any &, Any &); // template prototype
template <> void Swap<int>(job &, job &); // explicit specialization for job
int main(void)
{
template void Swap<char>(char &, char &); // explicit instantiation for char
short a, b;
...
Swap(a,b); // implicit template instantiation for short
job n, m;
...
Swap(n, m); // use explicit specialization for job
char g, h;
...
Swap(g, h); // use explicit template instantiation for char
...
}