<s>
Generic	O
programming	O
is	O
a	O
style	O
of	O
computer	B-General_Concept
programming	I-General_Concept
in	O
which	O
algorithms	O
are	O
written	O
in	O
terms	O
of	O
types	O
to-be-specified-later	O
that	O
are	O
then	O
instantiated	O
when	O
needed	O
for	O
specific	O
types	O
provided	O
as	O
parameters	O
.	O
</s>
<s>
This	O
approach	O
,	O
pioneered	O
by	O
the	O
ML	B-Language
programming	I-Language
language	I-Language
in	O
1973	O
,	O
permits	O
writing	O
common	O
functions	O
or	O
types	O
that	O
differ	O
only	O
in	O
the	O
set	O
of	O
types	O
on	O
which	O
they	O
operate	O
when	O
used	O
,	O
thus	O
reducing	O
duplication	O
.	O
</s>
<s>
Such	O
software	O
entities	O
are	O
known	O
as	O
generics	O
in	O
Ada	B-Language
,	O
C#	B-Application
,	O
Delphi	B-Language
,	O
Eiffel	B-Language
,	O
F#	B-Operating_System
,	O
Java	B-Language
,	O
Nim	O
,	O
Python	B-Language
,	O
Go	B-Application
,	O
Rust	B-Application
,	O
Swift	B-Application
,	O
TypeScript	B-Language
and	O
Visual	B-Language
Basic	I-Language
.NET	I-Language
.	O
</s>
<s>
They	O
are	O
known	O
as	O
parametric	O
polymorphism	B-Application
in	O
ML	B-Language
,	O
Scala	B-Application
,	O
Julia	B-Application
,	O
and	O
Haskell	B-Language
(	O
the	O
Haskell	B-Language
community	O
also	O
uses	O
the	O
term	O
"	O
generic	O
"	O
for	O
a	O
related	O
but	O
somewhat	O
different	O
concept	O
)	O
;	O
templates	B-Application
in	O
C++	B-Language
and	O
D	B-Application
;	O
and	O
parameterized	O
types	O
in	O
the	O
influential	O
1994	O
book	O
Design	O
Patterns	O
.	O
</s>
<s>
The	O
term	O
"	O
generic	O
programming	O
"	O
was	O
originally	O
coined	O
by	O
David	O
Musser	O
and	O
Alexander	O
Stepanov	O
in	O
a	O
more	O
specific	O
sense	O
than	O
the	O
above	O
,	O
to	O
describe	O
a	O
programming	O
paradigm	O
whereby	O
fundamental	O
requirements	O
on	O
types	O
are	O
abstracted	O
from	O
across	O
concrete	O
examples	O
of	O
algorithms	O
and	O
data	O
structures	O
and	O
formalized	O
as	O
concepts	B-Application
,	O
with	O
generic	O
functions	O
implemented	O
in	O
terms	O
of	O
these	O
concepts	B-Application
,	O
typically	O
using	O
language	O
genericity	O
mechanisms	O
as	O
described	O
above	O
.	O
</s>
<s>
The	O
"	O
generic	O
programming	O
"	O
paradigm	O
is	O
an	O
approach	O
to	O
software	O
decomposition	O
whereby	O
fundamental	O
requirements	O
on	O
types	O
are	O
abstracted	O
from	O
across	O
concrete	O
examples	O
of	O
algorithms	O
and	O
data	O
structures	O
and	O
formalized	O
as	O
concepts	B-Application
,	O
analogously	O
to	O
the	O
abstraction	O
of	O
algebraic	O
theories	O
in	O
abstract	O
algebra	O
.	O
</s>
<s>
Early	O
examples	O
of	O
this	O
programming	O
approach	O
were	O
implemented	O
in	O
Scheme	B-Language
and	O
Ada	B-Language
,	O
although	O
the	O
best	O
known	O
example	O
is	O
the	O
Standard	B-Application
Template	I-Application
Library	I-Application
(	O
STL	B-Application
)	O
,	O
which	O
developed	O
a	O
theory	O
of	O
iterators	O
that	O
is	O
used	O
to	O
decouple	O
sequence	O
data	O
structures	O
and	O
the	O
algorithms	O
operating	O
on	O
them	O
.	O
</s>
<s>
singly	O
linked	B-Data_Structure
list	I-Data_Structure
,	O
vector	O
etc.	O
,	O
and	O
M	O
algorithms	O
to	O
operate	O
on	O
them	O
,	O
e.g.	O
</s>
<s>
Several	O
iterator	O
concepts	B-Application
are	O
specified	O
in	O
the	O
STL	B-Application
,	O
each	O
a	O
refinement	O
of	O
more	O
restrictive	O
concepts	B-Application
e.g.	O
</s>
<s>
suitable	O
for	O
a	O
singly	O
linked	B-Data_Structure
list	I-Data_Structure
or	O
a	O
stream	O
of	O
input	O
data	O
)	O
,	O
whereas	O
a	O
random-access	O
iterator	O
also	O
provides	O
direct	O
constant-time	O
access	O
to	O
any	O
element	O
of	O
the	O
sequence	O
(	O
e.g.	O
</s>
<s>
An	O
important	O
point	O
is	O
that	O
a	O
data	O
structure	O
will	O
return	O
a	O
model	O
of	O
the	O
most	O
general	O
concept	O
that	O
can	O
be	O
implemented	O
efficiently	O
—	O
computational	B-General_Concept
complexity	I-General_Concept
requirements	O
are	O
explicitly	O
part	O
of	O
the	O
concept	O
definition	O
.	O
</s>
<s>
Note	O
that	O
although	O
this	O
approach	O
often	O
utilizes	O
language	O
features	O
of	O
compile-time	B-Application
genericity/templates	O
,	O
it	O
is	O
in	O
fact	O
independent	O
of	O
particular	O
language-technical	O
details	O
.	O
</s>
<s>
The	O
approach	O
is	O
a	O
lightweight	O
generic	O
programming	O
approach	O
for	O
Haskell	B-Language
.	O
</s>
<s>
Genericity	O
facilities	O
have	O
existed	O
in	O
high-level	O
languages	O
since	O
at	O
least	O
the	O
1970s	O
in	O
languages	O
such	O
as	O
ML	B-Language
,	O
CLU	B-Language
and	O
Ada	B-Language
,	O
and	O
were	O
subsequently	O
adopted	O
by	O
many	O
object-based	B-Application
and	O
object-oriented	B-Language
languages	I-Language
,	O
including	O
BETA	B-Language
,	O
C++	B-Language
,	O
D	B-Application
,	O
Eiffel	B-Language
,	O
Java	B-Language
,	O
and	O
DEC	O
's	O
now	O
defunct	O
Trellis-Owl	B-General_Concept
language	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
Forth	B-Application
the	O
compiler	B-Language
can	O
execute	O
code	O
while	O
compiling	B-Language
and	O
one	O
can	O
create	O
new	O
compiler	B-Language
keywords	O
and	O
new	O
implementations	O
for	O
those	O
words	O
on	O
the	O
fly	O
.	O
</s>
<s>
It	O
has	O
few	O
words	O
that	O
expose	O
the	O
compiler	B-Language
behaviour	O
and	O
therefore	O
naturally	O
offers	O
genericity	O
capacities	O
that	O
,	O
however	O
,	O
are	O
not	O
referred	O
to	O
as	O
such	O
in	O
most	O
Forth	B-Application
texts	O
.	O
</s>
<s>
The	O
term	O
has	O
been	O
used	O
in	O
functional	B-Language
programming	I-Language
,	O
specifically	O
in	O
Haskell-like	B-Language
languages	O
,	O
which	O
use	O
a	O
structural	O
type	O
system	O
where	O
types	O
are	O
always	O
parametric	O
and	O
the	O
actual	O
code	O
on	O
those	O
types	O
is	O
generic	O
.	O
</s>
<s>
Arrays	O
and	O
structs	B-Application
can	O
be	O
viewed	O
as	O
predefined	O
generic	O
types	O
.	O
</s>
<s>
Every	O
usage	O
of	O
an	O
array	O
or	O
struct	B-Application
type	O
instantiates	O
a	O
new	O
concrete	O
type	O
,	O
or	O
reuses	O
a	O
previous	O
instantiated	O
type	O
.	O
</s>
<s>
Array	O
element	O
types	O
and	O
struct	B-Application
element	O
types	O
are	O
parameterized	O
types	O
,	O
which	O
are	O
used	O
to	O
instantiate	O
the	O
corresponding	O
generic	O
type	O
.	O
</s>
<s>
All	O
this	O
is	O
usually	O
built-in	O
in	O
the	O
compiler	B-Language
and	O
the	O
syntax	O
differs	O
from	O
other	O
generic	O
constructs	O
.	O
</s>
<s>
Some	O
extensible	B-Application
programming	I-Application
languages	I-Application
try	O
to	O
unify	O
built-in	O
and	O
user	O
defined	O
generic	O
types	O
.	O
</s>
<s>
When	O
creating	O
container	B-Application
classes	O
in	O
statically	O
typed	O
languages	O
,	O
it	O
is	O
inconvenient	O
to	O
write	O
specific	O
implementations	O
for	O
each	O
datatype	O
contained	O
,	O
especially	O
if	O
the	O
code	O
for	O
each	O
datatype	O
is	O
virtually	O
identical	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
C++	B-Language
,	O
this	O
duplication	O
of	O
code	O
can	O
be	O
circumvented	O
by	O
defining	O
a	O
class	O
template	B-Application
:	O
</s>
<s>
These	O
"	O
containers-of-type-T	O
"	O
,	O
commonly	O
called	O
templates	B-Application
,	O
allow	O
a	O
class	O
to	O
be	O
reused	O
with	O
different	O
datatypes	O
as	O
long	O
as	O
certain	O
contracts	O
such	O
as	O
subtypes	O
and	O
signature	O
are	O
kept	O
.	O
</s>
<s>
This	O
genericity	O
mechanism	O
should	O
not	O
be	O
confused	O
with	O
inclusion	B-Application
polymorphism	I-Application
,	O
which	O
is	O
the	O
algorithmic	O
usage	O
of	O
exchangeable	O
sub-classes	O
:	O
for	O
instance	O
,	O
a	O
list	O
of	O
objects	O
of	O
type	O
Moving_Object	O
containing	O
objects	O
of	O
type	O
Animal	O
and	O
Car	O
.	O
</s>
<s>
Templates	B-Application
can	O
also	O
be	O
used	O
for	O
type-independent	O
functions	O
as	O
in	O
the	O
Swap	O
example	O
below	O
:	O
</s>
<s>
The	O
C++	B-Application
template	I-Application
construct	O
used	O
above	O
is	O
widely	O
cited	O
as	O
the	O
genericity	O
construct	O
that	O
popularized	O
the	O
notion	O
among	O
programmers	O
and	O
language	O
designers	O
and	O
supports	O
many	O
generic	O
programming	O
idioms	O
.	O
</s>
<s>
The	O
D	B-Application
programming	I-Application
language	I-Application
also	O
offers	O
fully	O
generic-capable	O
templates	B-Application
based	O
on	O
the	O
C++	B-Language
precedent	O
but	O
with	O
a	O
simplified	O
syntax	O
.	O
</s>
<s>
The	O
Java	B-Language
programming	I-Language
language	I-Language
has	O
provided	O
genericity	O
facilities	O
syntactically	O
based	O
on	O
C++'s	O
since	O
the	O
introduction	O
of	O
J2SE	B-Language
5.0	O
.	O
</s>
<s>
C#	B-Application
2.0	O
,	O
Oxygene	B-Language
1.5	I-Language
(	O
also	O
known	O
as	O
Chrome	B-Language
)	O
and	O
Visual	B-Language
Basic	I-Language
.NET	I-Language
2005	I-Language
have	O
constructs	O
that	O
take	O
advantage	O
of	O
the	O
support	O
for	O
generics	O
present	O
in	O
the	O
Microsoft	B-Application
.NET	I-Application
Framework	I-Application
since	O
version	O
2.0	O
.	O
</s>
<s>
Ada	B-Language
has	O
had	O
generics	O
since	O
it	O
was	O
first	O
designed	O
in	O
1977	O
–	O
1980	O
.	O
</s>
<s>
Ada	B-Language
2005	I-Language
adds	O
a	O
comprehensive	O
generic	O
container	B-Application
library	O
to	O
the	O
standard	O
library	O
,	O
which	O
was	O
inspired	O
by	O
C++'s	O
standard	B-Application
template	I-Application
library	I-Application
.	O
</s>
<s>
It	O
is	O
possible	O
to	O
instantiate	O
generic	O
units	O
at	O
run-time	B-Library
,	O
for	O
example	O
inside	O
a	O
loop	O
.	O
</s>
<s>
For	O
example	O
,	O
it	O
is	O
possible	O
to	O
specify	O
that	O
a	O
generic	O
formal	O
type	O
will	O
only	O
accept	O
a	O
modular	B-Architecture
type	O
as	O
the	O
actual	O
.	O
</s>
<s>
The	O
disadvantage	O
of	O
this	O
fine-grained	O
control	O
is	O
a	O
complicated	O
syntax	O
,	O
but	O
,	O
because	O
all	O
generic	O
formal	O
parameters	O
are	O
completely	O
defined	O
in	O
the	O
specification	O
,	O
the	O
compiler	B-Language
can	O
instantiate	O
generics	O
without	O
looking	O
at	O
the	O
body	O
of	O
the	O
generic	O
.	O
</s>
<s>
Unlike	O
C++	B-Language
,	O
Ada	B-Language
does	O
not	O
allow	O
specialised	O
generic	O
instances	O
,	O
and	O
requires	O
that	O
all	O
generics	O
be	O
instantiated	O
explicitly	O
.	O
</s>
<s>
the	O
compiler	B-Language
can	O
implement	O
shared	O
generics	O
:	O
the	O
object	O
code	O
for	O
a	O
generic	O
unit	O
can	O
be	O
shared	O
between	O
all	O
instances	O
(	O
unless	O
the	O
programmer	O
requests	O
inlining	O
of	O
subprograms	O
,	O
of	O
course	O
)	O
.	O
</s>
<s>
there	O
is	O
no	O
possibility	O
of	O
code	O
bloat	O
(	O
code	O
bloat	O
is	O
common	O
in	O
C++	B-Language
and	O
requires	O
special	O
care	O
,	O
as	O
explained	O
below	O
)	O
.	O
</s>
<s>
it	O
is	O
possible	O
to	O
instantiate	O
generics	O
at	O
run-time	B-Library
,	O
as	O
well	O
as	O
at	O
compile	B-Application
time	I-Application
,	O
since	O
no	O
new	O
object	O
code	O
is	O
required	O
for	O
a	O
new	O
instance	O
.	O
</s>
<s>
Ada	B-Language
does	O
not	O
permit	O
"	O
template	B-Language
metaprogramming	I-Language
"	O
,	O
because	O
it	O
does	O
not	O
allow	O
specialisations	O
.	O
</s>
<s>
C++	B-Language
uses	O
templates	B-Application
to	O
enable	O
generic	O
programming	O
techniques	O
.	O
</s>
<s>
The	O
C++	B-Language
Standard	O
Library	O
includes	O
the	O
Standard	B-Application
Template	I-Application
Library	I-Application
or	O
STL	B-Application
that	O
provides	O
a	O
framework	O
of	O
templates	B-Application
for	O
common	O
data	O
structures	O
and	O
algorithms	O
.	O
</s>
<s>
Templates	B-Application
in	O
C++	B-Language
may	O
also	O
be	O
used	O
for	O
template	B-Language
metaprogramming	I-Language
,	O
which	O
is	O
a	O
way	O
of	O
pre-evaluating	O
some	O
of	O
the	O
code	O
at	O
compile-time	B-Application
rather	O
than	O
run-time	B-Library
.	O
</s>
<s>
Using	O
template	B-Application
specialization	O
,	O
C++	B-Application
Templates	I-Application
are	O
considered	O
Turing	B-Algorithm
complete	I-Algorithm
.	O
</s>
<s>
There	O
are	O
many	O
kinds	O
of	O
templates	B-Application
,	O
the	O
most	O
common	O
being	O
function	O
templates	B-Application
and	O
class	O
templates	B-Application
.	O
</s>
<s>
A	O
function	O
template	B-Application
is	O
a	O
pattern	O
for	O
creating	O
ordinary	O
functions	O
based	O
upon	O
the	O
parameterizing	O
types	O
supplied	O
when	O
instantiated	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
C++	B-Language
Standard	B-Application
Template	I-Application
Library	I-Application
contains	O
the	O
function	O
template	B-Application
max(x, y )	O
that	O
creates	O
functions	O
that	O
return	O
either	O
x	O
or	O
y	O
,	O
whichever	O
is	O
larger	O
.	O
</s>
<s>
Specializations	O
of	O
this	O
function	O
template	B-Application
,	O
instantiations	O
with	O
specific	O
types	O
,	O
can	O
be	O
called	O
just	O
like	O
an	O
ordinary	O
function	O
:	O
</s>
<s>
The	O
compiler	B-Language
examines	O
the	O
arguments	O
used	O
to	O
call	O
max	O
and	O
determines	O
that	O
this	O
is	O
a	O
call	O
to	O
max(int, int )	O
.	O
</s>
<s>
A	O
program	O
defining	O
a	O
custom	O
data	O
type	O
can	O
use	O
operator	O
overloading	B-Application
to	O
define	O
the	O
meaning	O
of	O
<	O
for	O
that	O
type	O
,	O
thus	O
allowing	O
its	O
use	O
with	O
the	O
max( )	O
function	O
template	B-Application
.	O
</s>
<s>
While	O
this	O
may	O
seem	O
a	O
minor	O
benefit	O
in	O
this	O
isolated	O
example	O
,	O
in	O
the	O
context	O
of	O
a	O
comprehensive	O
library	O
like	O
the	O
STL	B-Application
it	O
allows	O
the	O
programmer	O
to	O
get	O
extensive	O
functionality	O
for	O
a	O
new	O
data	O
type	O
,	O
just	O
by	O
defining	O
a	O
few	O
operators	O
for	O
it	O
.	O
</s>
<s>
Merely	O
defining	O
<	O
allows	O
a	O
type	O
to	O
be	O
used	O
with	O
the	O
standard	O
sort( )	O
,	O
stable_sort( )	O
,	O
and	O
binary_search( )	O
algorithms	O
or	O
to	O
be	O
put	O
inside	O
data	O
structures	O
such	O
as	O
sets	O
,	O
heaps	O
,	O
and	O
associative	B-Application
arrays	I-Application
.	O
</s>
<s>
C++	B-Application
templates	I-Application
are	O
completely	O
type	B-Language
safe	I-Language
at	O
compile	B-Application
time	I-Application
.	O
</s>
<s>
Therefore	O
,	O
max(x, y )	O
will	O
fail	O
with	O
a	O
compile	B-Language
error	O
,	O
if	O
x	O
and	O
y	O
are	O
complex	O
values	O
.	O
</s>
<s>
Likewise	O
,	O
other	O
templates	B-Application
that	O
rely	O
on	O
<	O
cannot	O
be	O
applied	O
to	O
complex	O
data	O
unless	O
a	O
comparison	O
(	O
in	O
the	O
form	O
of	O
a	O
functor	O
or	O
function	O
)	O
is	O
provided	O
.	O
</s>
<s>
Unfortunately	O
,	O
compilers	B-Language
historically	O
generate	O
somewhat	O
esoteric	O
,	O
long	O
,	O
and	O
unhelpful	O
error	O
messages	O
for	O
this	O
sort	O
of	O
error	O
.	O
</s>
<s>
Another	O
kind	O
of	O
template	B-Application
,	O
a	O
class	O
template	B-Application
,	O
extends	O
the	O
same	O
concept	O
to	O
classes	O
.	O
</s>
<s>
A	O
class	O
template	B-Application
specialization	O
is	O
a	O
class	O
.	O
</s>
<s>
Class	O
templates	B-Application
are	O
often	O
used	O
to	O
make	O
generic	O
containers	B-Application
.	O
</s>
<s>
For	O
example	O
,	O
the	O
STL	B-Application
has	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
container	B-Application
.	O
</s>
<s>
To	O
make	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
of	O
integers	O
,	O
one	O
writes	O
listxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1	O
.	O
</s>
<s>
A	O
powerful	O
feature	O
of	O
C++'s	O
templates	B-Application
is	O
template	B-Application
specialization	O
.	O
</s>
<s>
Template	B-Application
specialization	O
has	O
two	O
purposes	O
:	O
to	O
allow	O
certain	O
forms	O
of	O
optimization	O
,	O
and	O
to	O
reduce	O
code	O
bloat	O
.	O
</s>
<s>
For	O
example	O
,	O
consider	O
a	O
sort( )	O
template	B-Application
function	O
.	O
</s>
<s>
One	O
of	O
the	O
primary	O
activities	O
that	O
such	O
a	O
function	O
does	O
is	O
to	O
swap	O
or	O
exchange	O
the	O
values	O
in	O
two	O
of	O
the	O
container	B-Application
's	O
positions	O
.	O
</s>
<s>
Template	B-Application
specialization	O
allows	O
the	O
template	B-Application
creator	O
to	O
write	O
different	O
implementations	O
and	O
to	O
specify	O
the	O
characteristics	O
that	O
the	O
parameterized	O
type(s )	O
must	O
have	O
for	O
each	O
implementation	O
to	O
be	O
used	O
.	O
</s>
<s>
Unlike	O
function	O
templates	B-Application
,	O
class	O
templates	B-Application
can	O
be	O
partially	B-Language
specialized	I-Language
.	O
</s>
<s>
That	O
means	O
that	O
an	O
alternate	O
version	O
of	O
the	O
class	O
template	B-Application
code	O
can	O
be	O
provided	O
when	O
some	O
of	O
the	O
template	B-Application
parameters	O
are	O
known	O
,	O
while	O
leaving	O
other	O
template	B-Application
parameters	O
generic	O
.	O
</s>
<s>
Clients	O
of	O
such	O
a	O
class	O
template	B-Application
just	O
use	O
specializations	O
of	O
it	O
without	O
needing	O
to	O
know	O
whether	O
the	O
compiler	B-Language
used	O
the	O
primary	O
specialization	O
or	O
some	O
partial	O
specialization	O
in	O
each	O
case	O
.	O
</s>
<s>
Class	O
templates	B-Application
can	O
also	O
be	O
fully	O
specialized	O
,	O
which	O
means	O
that	O
an	O
alternate	O
implementation	O
can	O
be	O
provided	O
when	O
all	O
of	O
the	O
parameterizing	O
types	O
are	O
known	O
.	O
</s>
<s>
Some	O
uses	O
of	O
templates	B-Application
,	O
such	O
as	O
the	O
max( )	O
function	O
,	O
were	O
previously	O
filled	O
by	O
function-like	O
preprocessor	B-General_Concept
macros	O
(	O
a	O
legacy	O
of	O
the	O
C	B-Language
programming	I-Language
language	I-Language
)	O
.	O
</s>
<s>
Macros	O
are	O
expanded	O
(	O
copy	O
pasted	O
)	O
by	O
preprocessor	B-General_Concept
,	O
before	O
compilation	B-Language
proper	O
;	O
templates	B-Application
are	O
actual	O
real	O
functions	O
.	O
</s>
<s>
Macros	O
are	O
always	O
expanded	O
inline	O
;	O
templates	B-Application
can	O
also	O
be	O
inline	O
functions	O
when	O
the	O
compiler	B-Language
deems	O
it	O
appropriate	O
.	O
</s>
<s>
However	O
,	O
templates	B-Application
are	O
generally	O
considered	O
an	O
improvement	O
over	O
macros	O
for	O
these	O
purposes	O
.	O
</s>
<s>
Templates	B-Application
are	O
type-safe	B-Language
.	O
</s>
<s>
Templates	B-Application
avoid	O
some	O
of	O
the	O
common	O
errors	O
found	O
in	O
code	O
that	O
makes	O
heavy	O
use	O
of	O
function-like	O
macros	O
,	O
such	O
as	O
evaluating	O
parameters	O
with	O
side	O
effects	O
twice	O
.	O
</s>
<s>
Perhaps	O
most	O
importantly	O
,	O
templates	B-Application
were	O
designed	O
to	O
be	O
applicable	O
to	O
much	O
larger	O
problems	O
than	O
macros	O
.	O
</s>
<s>
There	O
are	O
four	O
primary	O
drawbacks	O
to	O
the	O
use	O
of	O
templates	B-Application
:	O
supported	O
features	O
,	O
compiler	B-Language
support	O
,	O
poor	O
error	O
messages	O
(	O
usually	O
with	O
pre	O
C++20	O
SFINAE	B-Language
)	O
,	O
and	O
code	O
bloat	O
:	O
</s>
<s>
Templates	B-Application
in	O
C++	B-Language
lack	O
many	O
features	O
,	O
which	O
makes	O
implementing	O
them	O
and	O
using	O
them	O
in	O
a	O
straightforward	O
way	O
often	O
impossible	O
.	O
</s>
<s>
Current	O
developments	O
in	O
the	O
C++	B-Language
standards	O
exacerbate	O
this	O
problem	O
by	O
making	O
heavy	O
use	O
of	O
these	O
tricks	O
and	O
building	O
a	O
lot	O
of	O
new	O
features	O
for	O
templates	B-Application
on	O
them	O
or	O
with	O
them	O
in	O
mind	O
.	O
</s>
<s>
Many	O
compilers	B-Language
historically	O
had	O
poor	O
support	O
for	O
templates	B-Application
,	O
thus	O
the	O
use	O
of	O
templates	B-Application
could	O
have	O
made	O
code	O
somewhat	O
less	O
portable	O
.	O
</s>
<s>
Support	O
may	O
also	O
be	O
poor	O
when	O
a	O
C++	B-Language
compiler	B-Language
is	O
being	O
used	O
with	O
a	O
linker	B-Application
that	O
is	O
not	O
C++	B-Language
-aware	O
,	O
or	O
when	O
attempting	O
to	O
use	O
templates	B-Application
across	O
shared	O
library	O
boundaries	O
.	O
</s>
<s>
Compilers	B-Language
can	O
produce	O
confusing	O
,	O
long	O
,	O
and	O
sometimes	O
unhelpful	O
error	O
messages	O
when	O
errors	O
are	O
detected	O
in	O
code	O
that	O
uses	O
SFINAE	B-Language
.	O
</s>
<s>
This	O
can	O
make	O
templates	B-Application
difficult	O
to	O
develop	O
with	O
.	O
</s>
<s>
Finally	O
,	O
the	O
use	O
of	O
templates	B-Application
requires	O
the	O
compiler	B-Language
to	O
generate	O
a	O
separate	O
instance	O
of	O
the	O
templated	O
class	O
or	O
function	O
for	O
every	O
permutation	B-Algorithm
of	O
type	O
parameters	O
used	O
with	O
it	O
.	O
</s>
<s>
(	O
This	O
is	O
necessary	O
because	O
types	O
in	O
C++	B-Language
are	O
not	O
all	O
the	O
same	O
size	O
,	O
and	O
the	O
sizes	O
of	O
data	O
fields	O
are	O
important	O
to	O
how	O
classes	O
work	O
.	O
)	O
</s>
<s>
So	O
the	O
indiscriminate	O
use	O
of	O
templates	B-Application
can	O
lead	O
to	O
code	O
bloat	O
,	O
resulting	O
in	O
excessively	O
large	O
executables	O
.	O
</s>
<s>
However	O
,	O
judicious	O
use	O
of	O
template	B-Application
specialization	O
and	O
derivation	O
can	O
dramatically	O
reduce	O
such	O
code	O
bloat	O
in	O
some	O
cases	O
:	O
</s>
<s>
The	O
extra	O
instantiations	O
generated	O
by	O
templates	B-Application
can	O
also	O
cause	O
some	O
debuggers	O
to	O
have	O
difficulty	O
working	O
gracefully	O
with	O
templates	B-Application
.	O
</s>
<s>
For	O
example	O
,	O
setting	O
a	O
debug	O
breakpoint	O
within	O
a	O
template	B-Application
from	O
a	O
source	O
file	O
may	O
either	O
miss	O
setting	O
the	O
breakpoint	O
in	O
the	O
actual	O
instantiation	O
desired	O
or	O
may	O
set	O
a	O
breakpoint	O
in	O
every	O
place	O
the	O
template	B-Application
is	O
instantiated	O
.	O
</s>
<s>
Also	O
,	O
the	O
implementation	O
source	O
code	O
for	O
the	O
template	B-Application
must	O
be	O
completely	O
available	O
(	O
e.g.	O
</s>
<s>
Templates	B-Application
,	O
including	O
much	O
of	O
the	O
Standard	O
Library	O
,	O
if	O
not	O
included	O
in	O
header	O
files	O
,	O
cannot	O
be	O
compiled	B-Language
.	O
</s>
<s>
(	O
This	O
is	O
in	O
contrast	O
to	O
non-templated	O
code	O
,	O
which	O
may	O
be	O
compiled	B-Language
to	O
binary	O
,	O
providing	O
only	O
a	O
declarations	O
header	O
file	O
for	O
code	O
using	O
it	O
.	O
)	O
</s>
<s>
The	O
D	B-Application
programming	I-Application
language	I-Application
supports	O
templates	B-Application
based	O
in	O
design	O
on	O
C++	B-Language
.	O
</s>
<s>
Most	O
C++	B-Application
template	I-Application
idioms	O
will	O
carry	O
over	O
to	O
D	B-Application
without	O
alteration	O
,	O
but	O
D	B-Application
adds	O
some	O
additional	O
functionality	O
:	O
</s>
<s>
Template	B-Application
parameters	O
in	O
D	B-Application
are	O
not	O
restricted	O
to	O
just	O
types	O
and	O
primitive	O
values	O
(	O
as	O
it	O
was	O
in	O
C++	B-Language
before	O
C++20	O
)	O
,	O
but	O
also	O
allow	O
arbitrary	O
compile-time	B-Application
values	O
(	O
such	O
as	O
strings	O
and	O
struct	B-Application
literals	O
)	O
,	O
and	O
aliases	O
to	O
arbitrary	O
identifiers	O
,	O
including	O
other	O
templates	B-Application
or	O
template	B-Application
instantiations	O
.	O
</s>
<s>
Template	B-Application
constraints	O
and	O
the	O
static	O
if	O
statement	O
provide	O
an	O
alternative	O
to	O
respectively	O
C++'s	O
C++	B-Language
concepts	I-Language
and	O
if	O
constexpr	O
.	O
</s>
<s>
The	O
is( 	O
...	O
)	O
expression	O
allows	O
speculative	O
instantiation	O
to	O
verify	O
an	O
object	O
's	O
traits	O
at	O
compile	B-Application
time	I-Application
.	O
</s>
<s>
Templates	B-Application
in	O
D	B-Application
use	O
a	O
different	O
syntax	O
than	O
in	O
C++	B-Language
:	O
whereas	O
in	O
C++	B-Application
template	I-Application
parameters	O
are	O
wrapped	O
in	O
angular	O
brackets	O
(	O
Templatexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1	O
)	O
,	O
</s>
<s>
D	B-Application
uses	O
an	O
exclamation	O
sign	O
and	O
parentheses	O
:	O
Template	B-Application
!	O
</s>
<s>
This	O
avoids	O
the	O
C++	B-Language
parsing	O
difficulties	O
due	O
to	O
ambiguity	O
with	O
comparison	O
operators	O
.	O
</s>
<s>
Conventionally	O
,	O
D	B-Application
combines	O
the	O
above	O
features	O
to	O
provide	O
compile-time	B-Language
polymorphism	I-Language
using	O
trait-based	O
generic	O
programming	O
.	O
</s>
<s>
A	O
function	O
that	O
accepts	O
only	O
input	O
ranges	O
can	O
then	O
use	O
the	O
above	O
template	B-Application
in	O
a	O
template	B-Application
constraint	O
:	O
</s>
<s>
In	O
addition	O
to	O
template	B-Language
metaprogramming	I-Language
,	O
D	B-Application
also	O
provides	O
several	O
features	O
to	O
enable	O
compile-time	B-Application
code	O
generation	O
:	O
</s>
<s>
Compile-time	B-Language
reflection	I-Language
allows	O
enumerating	O
and	O
inspecting	O
declarations	O
and	O
their	O
members	O
during	O
compilation	B-Language
.	O
</s>
<s>
User-defined	O
attributes	B-General_Concept
allow	O
users	O
to	O
attach	O
arbitrary	O
identifiers	O
to	O
declarations	O
,	O
which	O
can	O
then	O
be	O
enumerated	O
using	O
compile-time	B-Language
reflection	I-Language
.	O
</s>
<s>
Compile-Time	B-Application
Function	O
Execution	O
(	O
CTFE	O
)	O
allows	O
a	O
subset	O
of	O
D	B-Application
(	O
restricted	O
to	O
safe	O
operations	O
)	O
to	O
be	O
interpreted	O
during	O
compilation	B-Language
.	O
</s>
<s>
String	O
mixins	O
allow	O
evaluating	O
and	O
compiling	B-Language
the	O
contents	O
of	O
a	O
string	O
expression	O
as	O
D	B-Application
code	O
that	O
becomes	O
part	O
of	O
the	O
program	O
.	O
</s>
<s>
User-defined	O
attributes	B-General_Concept
could	O
further	O
indicate	O
serialization	O
rules	O
.	O
</s>
<s>
The	O
import	O
expression	O
and	O
compile-time	B-Application
function	O
execution	O
also	O
allow	O
efficiently	O
implementing	O
domain-specific	B-Language
languages	I-Language
.	O
</s>
<s>
For	O
example	O
,	O
given	O
a	O
function	O
that	O
takes	O
a	O
string	O
containing	O
an	O
HTML	O
template	B-Application
and	O
returns	O
equivalent	O
D	B-Application
source	O
code	O
,	O
it	O
is	O
possible	O
to	O
use	O
it	O
in	O
the	O
following	O
way	O
:	O
</s>
<s>
Generic	O
classes	O
have	O
been	O
a	O
part	O
of	O
Eiffel	B-Language
since	O
the	O
original	O
method	O
and	O
language	O
design	O
.	O
</s>
<s>
The	O
foundation	O
publications	O
of	O
Eiffel	B-Language
,	O
use	O
the	O
term	O
genericity	O
to	O
describe	O
the	O
creation	O
and	O
use	O
of	O
generic	O
classes	O
.	O
</s>
<s>
Within	O
the	O
Eiffel	B-Language
type	O
system	O
,	O
although	O
class	O
LIST	O
 [ G ] 	O
is	O
considered	O
a	O
class	O
,	O
it	O
is	O
not	O
considered	O
a	O
type	O
.	O
</s>
<s>
Support	O
for	O
the	O
generics	O
,	O
or	O
"	O
containers-of-type-T	O
"	O
was	O
added	O
to	O
the	O
Java	B-Language
programming	I-Language
language	I-Language
in	O
2004	O
as	O
part	O
of	O
J2SE	B-Language
5.0	O
.	O
</s>
<s>
In	O
Java	B-Language
,	O
generics	O
are	O
only	O
checked	O
at	O
compile	B-Application
time	I-Application
for	O
type	O
correctness	O
.	O
</s>
<s>
The	O
generic	O
type	O
information	O
is	O
then	O
removed	O
via	O
a	O
process	O
called	O
type	B-General_Concept
erasure	I-General_Concept
,	O
to	O
maintain	O
compatibility	O
with	O
old	O
JVM	O
implementations	O
,	O
making	O
it	O
unavailable	O
at	B-Library
runtime	I-Library
.	O
</s>
<s>
The	O
compiler	B-Language
inserts	O
type	O
casts	O
to	O
convert	O
the	O
elements	O
to	O
the	O
String	O
type	O
when	O
they	O
are	O
retrieved	O
from	O
the	O
list	O
,	O
reducing	O
performance	O
compared	O
to	O
other	O
implementations	O
such	O
as	O
C++	B-Application
templates	I-Application
.	O
</s>
<s>
Generics	O
were	O
added	O
as	O
part	O
of	O
.NET	B-Application
Framework	I-Application
2.0	O
in	O
November	O
2005	O
,	O
based	O
on	O
a	O
research	O
prototype	O
from	O
Microsoft	O
Research	O
started	O
in	O
1999	O
.	O
</s>
<s>
Although	O
similar	O
to	O
generics	O
in	O
Java	B-Language
,	O
.NET	B-Application
generics	O
do	O
not	O
apply	O
type	B-General_Concept
erasure	I-General_Concept
,	O
but	O
implement	O
generics	O
as	O
a	O
first	O
class	O
mechanism	O
in	O
the	O
runtime	B-Library
using	O
reification	O
.	O
</s>
<s>
This	O
design	O
choice	O
provides	O
additional	O
functionality	O
,	O
such	O
as	O
allowing	O
reflection	B-Language
with	O
preservation	O
of	O
generic	O
types	O
,	O
as	O
well	O
as	O
alleviating	O
some	O
of	O
the	O
limitations	O
of	O
erasure	O
(	O
such	O
as	O
being	O
unable	O
to	O
create	O
generic	O
arrays	O
)	O
.	O
</s>
<s>
This	O
also	O
means	O
that	O
there	O
is	O
no	O
performance	O
hit	O
from	O
runtime	B-Library
casts	O
and	O
normally	O
expensive	O
boxing	O
conversions	O
.	O
</s>
<s>
When	O
primitive	O
and	O
value	O
types	O
are	O
used	O
as	O
generic	O
arguments	O
,	O
they	O
get	O
specialized	O
implementations	O
,	O
allowing	O
for	O
efficient	O
generic	O
collections	B-Application
and	O
methods	O
.	O
</s>
<s>
As	O
in	O
C++	B-Language
and	O
Java	B-Language
,	O
nested	O
generic	O
types	O
such	O
as	O
Dictionaryxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1	O
are	O
valid	O
types	O
,	O
however	O
are	O
advised	O
against	O
for	O
member	O
signatures	O
in	O
code	O
analysis	O
design	O
rules	O
.	O
</s>
<s>
.NET	B-Application
allows	O
six	O
varieties	O
of	O
generic	O
type	O
constraints	O
using	O
the	O
where	O
keyword	O
including	O
restricting	O
generic	O
types	O
to	O
be	O
value	O
types	O
,	O
to	O
be	O
classes	O
,	O
to	O
have	O
constructors	O
,	O
and	O
to	O
implement	O
interfaces	O
.	O
</s>
<s>
This	O
ensures	O
a	O
compile	B-Application
time	I-Application
error	O
,	O
if	O
the	O
method	O
is	O
called	O
if	O
the	O
type	O
does	O
not	O
support	O
comparison	O
.	O
</s>
<s>
However	O
,	O
since	O
arrays	O
are	O
contravariant	O
,	O
the	O
casting	O
would	O
not	O
be	O
type	B-Language
safe	I-Language
,	O
and	O
the	O
compiler	B-Language
would	O
be	O
unable	O
to	O
find	O
certain	O
possible	O
errors	O
that	O
would	O
otherwise	O
be	O
caught	O
when	O
using	O
generic	O
types	O
.	O
</s>
<s>
A	O
notable	O
behavior	O
of	O
static	O
members	O
in	O
a	O
generic	O
.NET	B-Application
class	O
is	O
static	O
member	O
instantiation	O
per	O
run-time	B-Library
type	O
(	O
see	O
example	O
below	O
)	O
.	O
</s>
<s>
Delphi	B-Language
's	I-Language
Object	O
Pascal	O
dialect	O
acquired	O
generics	O
in	O
the	O
Delphi	B-Language
2007	O
release	O
,	O
initially	O
only	O
with	O
the	O
(	O
now	O
discontinued	O
)	O
.NET	B-Application
compiler	B-Language
before	O
being	O
added	O
to	O
the	O
native	O
code	O
in	O
the	O
Delphi	B-Language
2009	I-Language
release	O
.	O
</s>
<s>
The	O
semantics	O
and	O
capabilities	O
of	O
Delphi	B-Language
generics	O
are	O
largely	O
modelled	O
on	O
those	O
had	O
by	O
generics	O
in	O
.NET	B-Application
2.0	O
,	O
though	O
the	O
implementation	O
is	O
by	O
necessity	O
quite	O
different	O
.	O
</s>
<s>
Here	O
's	O
a	O
more	O
or	O
less	O
direct	O
translation	O
of	O
the	O
first	O
C#	B-Application
example	O
shown	O
above	O
:	O
</s>
<s>
As	O
with	O
C#	B-Application
,	O
methods	O
as	O
well	O
as	O
whole	O
types	O
can	O
have	O
one	O
or	O
more	O
type	O
parameters	O
.	O
</s>
<s>
The	O
available	O
constraints	O
are	O
very	O
similar	O
to	O
the	O
available	O
constraints	O
in	O
C#	B-Application
:	O
any	O
value	O
type	O
,	O
any	O
class	O
,	O
a	O
specific	O
class	O
or	O
interface	O
,	O
and	O
a	O
class	O
with	O
a	O
parameterless	O
constructor	O
.	O
</s>
<s>
Free	B-Operating_System
Pascal	I-Operating_System
implemented	O
generics	O
before	O
Delphi	B-Language
,	O
and	O
with	O
different	O
syntax	O
and	O
semantics	O
.	O
</s>
<s>
However	O
,	O
since	O
FPC	O
version	O
2.6.0	O
,	O
the	O
Delphi-style	O
syntax	O
is	O
available	O
when	O
using	O
the	O
{$mode	O
Delphi}	O
language	O
mode	O
.	O
</s>
<s>
Thus	O
,	O
Free	B-Operating_System
Pascal	I-Operating_System
programmers	O
are	O
able	O
to	O
use	O
generics	O
in	O
whichever	O
style	O
they	O
prefer	O
.	O
</s>
<s>
Delphi	B-Language
and	O
Free	B-Operating_System
Pascal	I-Operating_System
example	O
:	O
</s>
<s>
The	O
type	O
class	O
mechanism	O
of	O
Haskell	B-Language
supports	O
generic	O
programming	O
.	O
</s>
<s>
Six	O
of	O
the	O
predefined	O
type	O
classes	O
in	O
Haskell	B-Language
(	O
including	O
Eq	O
,	O
the	O
types	O
that	O
can	O
be	O
compared	O
for	O
equality	O
,	O
and	O
Show	O
,	O
the	O
types	O
whose	O
values	O
can	O
be	O
rendered	O
as	O
strings	O
)	O
have	O
the	O
special	O
property	O
of	O
supporting	O
derived	O
instances	O
.	O
</s>
<s>
The	O
support	O
for	O
derived	O
instances	O
of	O
Eq	O
and	O
Show	O
makes	O
their	O
methods	O
==	O
and	O
show	O
generic	O
in	O
a	O
qualitatively	O
different	O
way	O
from	O
parametrically	O
polymorphic	B-Application
functions	I-Application
:	O
these	O
"	O
functions	O
"	O
(	O
more	O
accurately	O
,	O
type-indexed	O
families	O
of	O
functions	O
)	O
can	O
be	O
applied	O
to	O
values	O
of	O
various	O
types	O
,	O
and	O
although	O
they	O
behave	O
differently	O
for	O
every	O
argument	O
type	O
,	O
little	O
work	O
is	O
needed	O
to	O
add	O
support	O
for	O
a	O
new	O
type	O
.	O
</s>
<s>
Other	O
researchers	O
have	O
proposed	O
approaches	O
to	O
this	O
and	O
other	O
kinds	O
of	O
genericity	O
in	O
the	O
context	O
of	O
Haskell	B-Language
and	O
extensions	O
to	O
Haskell	B-Language
(	O
discussed	O
below	O
)	O
.	O
</s>
<s>
PolyP	O
was	O
the	O
first	O
generic	O
programming	O
language	O
extension	O
to	O
Haskell	B-Language
.	O
</s>
<s>
Regular	O
datatypes	O
in	O
PolyP	O
are	O
a	O
subset	O
of	O
Haskell	B-Language
datatypes	O
.	O
</s>
<s>
Generic	O
Haskell	B-Language
is	O
another	O
extension	O
to	O
Haskell	B-Language
,	O
developed	O
at	O
Utrecht	O
University	O
in	O
the	O
Netherlands	O
.	O
</s>
<s>
Type-indexed	O
values	O
are	O
defined	O
as	O
a	O
value	O
indexed	O
over	O
the	O
various	O
Haskell	B-Language
type	O
constructors	O
(	O
unit	O
,	O
primitive	O
types	O
,	O
sums	O
,	O
products	O
,	O
and	O
user-defined	O
type	O
constructors	O
)	O
.	O
</s>
<s>
Clean	B-Operating_System
offers	O
generic	O
programming	O
based	O
and	O
the	O
as	O
supported	O
by	O
the	O
GHC	O
≥	O
6.0	O
.	O
</s>
<s>
It	O
parametrizes	O
by	O
kind	O
as	O
those	O
but	O
offers	O
overloading	B-Application
.	O
</s>
<s>
Languages	O
in	O
the	O
ML	B-Language
family	O
support	O
generic	O
programming	O
through	O
parametric	O
polymorphism	B-Application
and	O
generic	O
modules	B-Architecture
called	O
functors	O
.	O
</s>
<s>
Both	O
Standard	B-Language
ML	I-Language
and	O
OCaml	B-Language
provide	O
functors	O
,	O
which	O
are	O
similar	O
to	O
class	O
templates	B-Application
and	O
to	O
Ada	B-Language
's	O
generic	O
packages	O
.	O
</s>
<s>
Scheme	B-Language
syntactic	O
abstractions	O
also	O
have	O
a	O
connection	O
to	O
genericity	O
–	O
these	O
are	O
in	O
fact	O
a	O
superset	O
of	O
C++	B-Application
templates	I-Application
.	O
</s>
<s>
A	O
Verilog	B-Language
module	B-Architecture
may	O
take	O
one	O
or	O
more	O
parameters	O
,	O
to	O
which	O
their	O
actual	O
values	O
are	O
assigned	O
upon	O
the	O
instantiation	O
of	O
the	O
module	B-Architecture
.	O
</s>
<s>
One	O
example	O
is	O
a	O
generic	O
register	B-General_Concept
array	O
where	O
the	O
array	O
width	O
is	O
given	O
via	O
a	O
parameter	O
.	O
</s>
<s>
Such	O
an	O
array	O
,	O
combined	O
with	O
a	O
generic	O
wire	O
vector	O
,	O
can	O
make	O
a	O
generic	O
buffer	O
or	O
memory	O
module	B-Architecture
with	O
an	O
arbitrary	O
bit	O
width	O
out	O
of	O
a	O
single	O
module	B-Architecture
implementation.Verilog	O
by	O
Example	O
,	O
Section	O
The	O
Rest	O
for	O
Reference	O
.	O
</s>
<s>
Blaine	O
C	B-Language
.	O
Readler	O
,	O
Full	O
Arc	O
Press	O
,	O
2011	O
.	O
</s>
<s>
VHDL	B-Language
,	O
being	O
derived	O
from	O
Ada	B-Language
,	O
also	O
has	O
generic	O
capabilities	O
.	O
</s>
<s>
David	O
Vandevoorde	O
,	O
Nicolai	O
M	O
Josuttis	O
,	O
C++	B-Application
Templates	I-Application
:	O
The	O
Complete	O
Guide	O
,	O
2003	O
Addison-Wesley	O
.	O
</s>
<s>
An	O
open	O
source	O
generics	O
library	O
for	O
C#	B-Application
.	O
</s>
<s>
Nick	O
Hodges	O
,	O
"	O
Delphi	B-Language
2009	I-Language
Reviewers	O
Guide	O
,	O
"	O
October	O
2008	O
,	O
Embarcadero	O
Developer	O
Network	O
,	O
Embarcadero	O
.	O
</s>
<s>
Libraries	O
for	O
Generic	O
Programming	O
in	O
Haskell	B-Language
.	O
</s>
<s>
Ralf	O
Hinze	O
,	O
"	O
Generics	O
for	O
the	O
Masses	O
,	O
"	O
In	O
Proceedings	O
of	O
the	O
ACM	O
SIGPLAN	O
International	O
Conference	O
on	O
Functional	B-Language
Programming	I-Language
(	O
ICFP	O
)	O
,	O
2004	O
.	O
</s>
<s>
Maurice	O
Naftalin	O
and	O
Philip	O
Wadler	O
,	O
Java	B-Language
Generics	O
and	O
Collections	B-Application
,	O
2006	O
,	O
O'Reilly	O
Media	O
,	O
Inc	O
.	O
</s>
<s>
Peter	O
Sestoft	O
,	O
Java	B-Language
Precisely	O
,	O
Second	O
Edition	O
,	O
''	O
2005	O
MIT	O
Press	O
.	O
</s>
