<s>
Templates	B-Application
are	O
a	O
feature	O
of	O
the	O
C++	B-Language
programming	I-Language
language	I-Language
that	O
allows	O
functions	O
and	O
classes	O
to	O
operate	O
with	O
generic	B-Language
types	I-Language
.	O
</s>
<s>
The	O
C++	B-Language
Standard	I-Language
Library	I-Language
provides	O
many	O
useful	O
functions	O
within	O
a	O
framework	O
of	O
connected	O
templates	B-Application
.	O
</s>
<s>
Major	O
inspirations	O
for	O
C++	B-Application
templates	I-Application
were	O
the	O
parameterized	O
modules	O
provided	O
by	O
CLU	B-Language
and	O
the	O
generics	O
provided	O
by	O
Ada	B-Language
.	O
</s>
<s>
There	O
are	O
three	O
kinds	O
of	O
templates	B-Application
:	O
function	O
templates	B-Application
,	O
class	O
templates	B-Application
and	O
,	O
since	O
C++14	B-Language
,	O
variable	O
templates	B-Application
.	O
</s>
<s>
Since	O
C++11	B-Language
,	O
templates	B-Application
may	O
be	O
either	O
variadic	B-Language
or	O
non-variadic	O
;	O
in	O
earlier	O
versions	O
of	O
C++	B-Language
they	O
are	O
always	O
non-variadic	O
.	O
</s>
<s>
A	O
function	O
template	B-Application
behaves	O
like	O
a	O
function	O
except	O
that	O
the	O
template	B-Application
can	O
have	O
arguments	O
of	O
many	O
different	O
types	O
(	O
see	O
example	O
)	O
.	O
</s>
<s>
In	O
other	O
words	O
,	O
a	O
function	O
template	B-Application
represents	O
a	O
family	O
of	O
functions	O
.	O
</s>
<s>
The	O
format	O
for	O
declaring	O
function	O
templates	B-Application
with	O
type	O
parameters	O
is	O
:	O
</s>
<s>
The	O
latter	O
form	O
was	O
introduced	O
to	O
avoid	O
confusion	O
,	O
since	O
a	O
type	O
parameter	O
need	O
not	O
be	O
a	O
class	O
until	O
C++20	B-Language
.	O
</s>
<s>
For	O
example	O
,	O
the	O
C++	B-Language
Standard	I-Language
Library	I-Language
contains	O
the	O
function	O
template	B-Application
max(x, y )	O
which	O
returns	O
the	O
larger	O
of	O
x	O
and	O
y	O
.	O
</s>
<s>
That	O
function	O
template	B-Application
could	O
be	O
defined	O
like	O
this	O
:	O
</s>
<s>
The	O
usage	O
of	O
a	O
function	O
template	B-Application
saves	O
space	O
in	O
the	O
source	O
code	O
file	O
in	O
addition	O
to	O
limiting	O
changes	O
to	O
one	O
function	O
description	O
and	O
making	O
the	O
code	O
easier	O
to	O
read	O
.	O
</s>
<s>
A	O
template	B-Application
does	O
not	O
produce	O
smaller	O
object	O
code	O
,	O
though	O
,	O
compared	O
to	O
writing	O
separate	O
functions	O
for	O
all	O
the	O
different	O
data	O
types	O
used	O
in	O
a	O
specific	O
program	O
.	O
</s>
<s>
For	O
example	O
,	O
if	O
a	O
program	O
uses	O
both	O
an	O
int	O
and	O
a	O
double	O
version	O
of	O
the	O
max( )	O
function	O
template	B-Application
shown	O
above	O
,	O
the	O
compiler	O
will	O
create	O
an	O
object	O
code	O
version	O
of	O
max( )	O
that	O
operates	O
on	O
int	O
arguments	O
and	O
another	O
object	O
code	O
version	O
that	O
operates	O
on	O
double	O
arguments	O
.	O
</s>
<s>
Here	O
is	O
how	O
the	O
function	O
template	B-Application
could	O
be	O
used	O
:	O
</s>
<s>
In	O
the	O
first	O
two	O
cases	O
,	O
the	O
template	B-Application
argument	O
T	O
is	O
automatically	O
deduced	O
by	O
the	O
compiler	O
to	O
be	O
int	O
and	O
double	O
,	O
respectively	O
.	O
</s>
<s>
In	O
the	O
third	O
case	O
automatic	O
deduction	O
of	O
max( 3	O
,	O
7.0	O
)	O
would	O
fail	O
because	O
the	O
type	O
of	O
the	O
parameters	O
must	O
in	O
general	O
match	O
the	O
template	B-Application
arguments	O
exactly	O
.	O
</s>
<s>
This	O
function	O
template	B-Application
can	O
be	O
instantiated	O
with	O
any	O
copy-constructible	B-Language
type	O
for	O
which	O
the	O
expression	O
y	O
>	O
x	O
is	O
valid	O
.	O
</s>
<s>
A	O
class	O
template	B-Application
provides	O
a	O
specification	O
for	O
generating	O
classes	O
based	O
on	O
parameters	O
.	O
</s>
<s>
Class	O
templates	B-Application
are	O
generally	O
used	O
to	O
implement	O
containers	B-Application
.	O
</s>
<s>
A	O
class	O
template	B-Application
is	O
instantiated	O
by	O
passing	O
a	O
given	O
set	O
of	O
types	O
to	O
it	O
as	O
template	B-Application
arguments	O
.	O
</s>
<s>
The	O
C++	B-Language
Standard	I-Language
Library	I-Language
contains	O
many	O
class	O
templates	B-Application
,	O
in	O
particular	O
the	O
containers	B-Application
adapted	O
from	O
the	O
Standard	B-Application
Template	I-Application
Library	I-Application
,	O
such	O
as	O
vector	O
.	O
</s>
<s>
In	O
C++14	B-Language
,	O
templates	B-Application
can	O
be	O
also	O
used	O
for	O
variables	O
,	O
as	O
in	O
the	O
following	O
example	O
:	O
</s>
<s>
Although	O
templating	B-Language
on	O
types	O
,	O
as	O
in	O
the	O
examples	O
above	O
,	O
is	O
the	O
most	O
common	O
form	O
of	O
templating	B-Language
in	O
C++	B-Language
,	O
it	O
is	O
also	O
possible	O
to	O
template	B-Application
on	O
values	O
.	O
</s>
<s>
As	O
a	O
real-world	O
example	O
,	O
the	O
standard	B-Language
library	I-Language
fixed-size	O
array	O
type	O
std::array	O
is	O
templated	O
on	O
both	O
a	O
type	O
(	O
representing	O
the	O
type	O
of	O
object	O
that	O
the	O
array	O
holds	O
)	O
and	O
a	O
number	O
which	O
is	O
of	O
type	O
std::size_t	O
(	O
representing	O
the	O
number	O
of	O
elements	O
the	O
array	O
holds	O
)	O
.	O
</s>
<s>
When	O
a	O
function	O
or	O
class	O
is	O
instantiated	O
from	O
a	O
template	B-Application
,	O
a	O
specialization	O
of	O
that	O
template	B-Application
is	O
created	O
by	O
the	O
compiler	O
for	O
the	O
set	O
of	O
arguments	O
used	O
,	O
and	O
the	O
specialization	O
is	O
referred	O
to	O
as	O
being	O
a	O
generated	O
specialization	O
.	O
</s>
<s>
Sometimes	O
,	O
the	O
programmer	O
may	O
decide	O
to	O
implement	O
a	O
special	O
version	O
of	O
a	O
function	O
(	O
or	O
class	O
)	O
for	O
a	O
given	O
set	O
of	O
template	B-Application
type	O
arguments	O
which	O
is	O
called	O
an	O
explicit	O
specialization	O
.	O
</s>
<s>
In	O
this	O
way	O
certain	O
template	B-Application
types	O
can	O
have	O
a	O
specialized	O
implementation	O
that	O
is	O
optimized	O
for	O
the	O
type	O
or	O
a	O
more	O
meaningful	O
implementation	O
than	O
the	O
generic	O
implementation	O
.	O
</s>
<s>
If	O
a	O
class	O
template	B-Application
is	O
specialized	O
by	O
a	O
subset	O
of	O
its	O
parameters	O
it	O
is	O
called	O
partial	B-Language
template	I-Language
specialization	I-Language
(	O
function	O
templates	B-Application
cannot	O
be	O
partially	O
specialized	O
)	O
.	O
</s>
<s>
Explicit	O
specialization	O
is	O
used	O
when	O
the	O
behavior	O
of	O
a	O
function	O
or	O
class	O
for	O
particular	O
choices	O
of	O
the	O
template	B-Application
parameters	O
must	O
deviate	O
from	O
the	O
generic	O
behavior	O
:	O
that	O
is	O
,	O
from	O
the	O
code	O
generated	O
by	O
the	O
main	O
template	B-Application
,	O
or	O
templates	B-Application
.	O
</s>
<s>
For	O
example	O
,	O
the	O
template	B-Application
definition	O
below	O
defines	O
a	O
specific	O
implementation	O
of	O
max( )	O
for	O
arguments	O
of	O
type	O
const	O
char*	O
:	O
</s>
<s>
C++11	B-Language
introduced	O
variadic	B-Language
templates	I-Language
,	O
which	O
can	O
take	O
a	O
variable	O
number	O
of	O
arguments	O
in	O
a	O
manner	O
somewhat	O
similar	O
to	O
variadic	B-Language
functions	I-Language
such	O
as	O
std::printf	O
.	O
</s>
<s>
C++11	B-Language
introduced	O
template	B-Application
aliases	O
,	O
which	O
act	O
like	O
parameterized	O
typedefs	B-Language
.	O
</s>
<s>
The	O
following	O
code	O
shows	O
the	O
definition	O
of	O
a	O
template	B-Application
alias	O
StrMap	O
.	O
</s>
<s>
Initially	O
,	O
the	O
concept	O
of	O
templates	B-Application
was	O
not	O
included	O
in	O
some	O
languages	O
,	O
such	O
as	O
Java	B-Language
and	O
C#	B-Application
1.0	O
.	O
</s>
<s>
Java	B-Language
's	I-Language
adoption	I-Language
of	I-Language
generics	I-Language
mimics	O
the	O
behavior	O
of	O
templates	B-Application
,	O
but	O
is	O
technically	O
different	O
.	O
</s>
<s>
C#	B-Application
added	O
generics	O
(	O
parameterized	B-Language
types	I-Language
)	O
in	O
.NET	B-Application
2.0	O
.	O
</s>
<s>
The	O
generics	O
in	O
Ada	B-Language
predate	O
C++	B-Application
templates	I-Application
.	O
</s>
<s>
Although	O
C++	B-Application
templates	I-Application
,	O
Java	B-Language
generics	I-Language
,	O
and	O
.NET	B-Application
generics	O
are	O
often	O
considered	O
similar	O
,	O
generics	O
only	O
mimic	O
the	O
basic	O
behavior	O
of	O
C++	B-Application
templates	I-Application
.	O
</s>
<s>
Some	O
of	O
the	O
advanced	O
template	B-Application
features	O
utilized	O
by	O
libraries	O
such	O
as	O
Boost	B-Language
and	O
STLSoft	O
,	O
and	O
implementations	O
of	O
the	O
STL	O
itself	O
,	O
for	O
template	B-Language
metaprogramming	I-Language
(	O
explicit	O
or	O
partial	O
specialization	O
,	O
default	O
template	B-Application
arguments	O
,	O
template	B-Application
non-type	O
arguments	O
,	O
template	B-Application
template	B-Application
arguments	O
,	O
...	O
)	O
are	O
not	O
available	O
with	O
generics	O
.	O
</s>
<s>
In	O
C++	B-Application
templates	I-Application
,	O
compile-time	O
cases	O
were	O
historically	O
performed	O
by	O
pattern	O
matching	O
over	O
the	O
template	B-Application
arguments	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
template	B-Application
base	O
class	O
in	O
the	O
Factorial	O
example	O
below	O
is	O
implemented	O
by	O
matching	O
0	O
rather	O
than	O
with	O
an	O
inequality	O
test	O
,	O
which	O
was	O
previously	O
unavailable	O
.	O
</s>
<s>
However	O
,	O
the	O
arrival	O
in	O
C++11	B-Language
of	O
standard	B-Language
library	I-Language
features	O
such	O
as	O
std::conditional	O
has	O
provided	O
another	O
,	O
more	O
flexible	O
way	O
to	O
handle	O
conditional	O
template	B-Application
instantiation	O
.	O
</s>
<s>
Alternatively	O
,	O
constexpr	O
in	O
C++11	B-Language
/	O
consteval	O
in	O
C++20	B-Language
can	O
be	O
used	O
to	O
calculate	O
such	O
values	O
directly	O
using	O
a	O
function	O
at	O
compile-time	O
.	O
</s>
<s>
Because	O
of	O
this	O
,	O
template	B-Language
meta-programming	I-Language
is	O
now	O
mostly	O
used	O
to	O
do	O
operations	O
on	O
types	O
.	O
</s>
