Skip to content

Latest commit

 

History

History
46 lines (37 loc) · 1.36 KB

explicit-specialization-of-function-templates.md

File metadata and controls

46 lines (37 loc) · 1.36 KB
description title ms.date helpviewer_keywords ms.assetid
Learn more about: Explicit Specialization of Function Templates
Explicit Specialization of Function Templates
11/04/2016
overriding, functions
function templates, specialization
explicit specialization of function templates
declaring functions [C++], specialization of function template
specialization of function templates
eb0fcb73-eaed-42a1-9b83-14b055a34bf8

Explicit Specialization of Function Templates

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.

Example

// 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()
{
}

See also

Function Templates