description | title | ms.date | helpviewer_keywords | ms.assetid | |||||
---|---|---|---|---|---|---|---|---|---|
Learn more about: Explicit Specialization of Function Templates |
Explicit Specialization of Function Templates |
11/04/2016 |
|
eb0fcb73-eaed-42a1-9b83-14b055a34bf8 |
With a function template, you can define special behavior for a specific type by providing an explicit specialization (override) of the function template for that type. For example:
template<> void MySwap(double a, double b);
This declaration enables you to define a different function for double
variables. Like non-template functions, standard type conversions (such as promoting a variable of type float
to double
) are applied.
// explicit_specialization.cpp
template<class T> void f(T t)
{
};
// Explicit specialization of f with 'char' with the
// template argument explicitly specified:
//
template<> void f<char>(char c)
{
}
// Explicit specialization of f with 'double' with the
// template argument deduced:
//
template<> void f(double d)
{
}
int main()
{
}