<s>
Template	B-Language
metaprogramming	I-Language
(	O
TMP	O
)	O
is	O
a	O
metaprogramming	B-Application
technique	O
in	O
which	O
templates	B-Language
are	O
used	O
by	O
a	O
compiler	B-Language
to	O
generate	O
temporary	O
source	O
code	O
,	O
which	O
is	O
merged	O
by	O
the	O
compiler	B-Language
with	O
the	O
rest	O
of	O
the	O
source	O
code	O
and	O
then	O
compiled	B-Language
.	O
</s>
<s>
The	O
output	O
of	O
these	O
templates	B-Language
can	O
include	O
compile-time	B-Application
constants	O
,	O
data	B-General_Concept
structures	I-General_Concept
,	O
and	O
complete	O
functions	O
.	O
</s>
<s>
The	O
use	O
of	O
templates	B-Language
can	O
be	O
thought	O
of	O
as	O
compile-time	B-Application
polymorphism	B-Application
.	O
</s>
<s>
The	O
technique	O
is	O
used	O
by	O
a	O
number	O
of	O
languages	O
,	O
the	O
best-known	O
being	O
C++	B-Language
,	O
but	O
also	O
Curl	B-Language
,	O
D	B-Application
,	O
Nim	B-Application
,	O
and	O
XL	O
.	O
</s>
<s>
Template	B-Language
metaprogramming	I-Language
was	O
,	O
in	O
a	O
sense	O
,	O
discovered	O
accidentally	O
.	O
</s>
<s>
Some	O
other	O
languages	O
support	O
similar	O
,	O
if	O
not	O
more	O
powerful	O
,	O
compile-time	B-Application
facilities	O
(	O
such	O
as	O
Lisp	B-Language
macros	O
)	O
,	O
but	O
those	O
are	O
outside	O
the	O
scope	O
of	O
this	O
article	O
.	O
</s>
<s>
The	O
use	O
of	O
templates	B-Language
as	O
a	O
metaprogramming	B-Application
technique	O
requires	O
two	O
distinct	O
operations	O
:	O
a	O
template	O
must	O
be	O
defined	O
,	O
and	O
a	O
defined	O
template	O
must	O
be	O
instantiated	O
.	O
</s>
<s>
Template	B-Language
metaprogramming	I-Language
is	O
Turing-complete	O
,	O
meaning	O
that	O
any	O
computation	O
expressible	O
by	O
a	O
computer	O
program	O
can	O
be	O
computed	O
,	O
in	O
some	O
form	O
,	O
by	O
a	O
template	O
metaprogram	O
.	O
</s>
<s>
Templates	B-Language
are	O
different	O
from	O
macros	O
.	O
</s>
<s>
A	O
macro	O
is	O
a	O
piece	O
of	O
code	O
that	O
executes	O
at	O
compile	B-Application
time	I-Application
and	O
either	O
performs	O
textual	O
manipulation	O
of	O
code	O
to-be	O
compiled	B-Language
(	O
e.g.	O
</s>
<s>
C++	B-Language
macros	O
)	O
or	O
manipulates	O
the	O
abstract	B-Data_Structure
syntax	I-Data_Structure
tree	I-Data_Structure
being	O
produced	O
by	O
the	O
compiler	B-Language
(	O
e.g.	O
</s>
<s>
Rust	B-Application
or	O
Lisp	B-Language
macros	O
)	O
.	O
</s>
<s>
Textual	O
macros	O
are	O
notably	O
more	O
independent	O
of	O
the	O
syntax	O
of	O
the	O
language	O
being	O
manipulated	O
,	O
as	O
they	O
merely	O
change	O
the	O
in-memory	O
text	O
of	O
the	O
source	O
code	O
right	O
before	O
compilation	B-Language
.	O
</s>
<s>
Template	O
metaprograms	O
have	O
no	O
mutable	B-Application
variables	I-Application
that	O
is	O
,	O
no	O
variable	O
can	O
change	O
value	O
once	O
it	O
has	O
been	O
initialized	O
,	O
therefore	O
template	B-Language
metaprogramming	I-Language
can	O
be	O
seen	O
as	O
a	O
form	O
of	O
functional	B-Language
programming	I-Language
.	O
</s>
<s>
Though	O
the	O
syntax	O
of	O
template	B-Language
metaprogramming	I-Language
is	O
usually	O
very	O
different	O
from	O
the	O
programming	O
language	O
it	O
is	O
used	O
with	O
,	O
it	O
has	O
practical	O
uses	O
.	O
</s>
<s>
Some	O
common	O
reasons	O
to	O
use	O
templates	B-Language
are	O
to	O
implement	O
generic	B-Language
programming	I-Language
(	O
avoiding	O
sections	O
of	O
code	O
which	O
are	O
similar	O
except	O
for	O
some	O
minor	O
variations	O
)	O
or	O
to	O
perform	O
automatic	O
compile-time	B-Application
optimization	O
such	O
as	O
doing	O
something	O
once	O
at	O
compile	B-Application
time	I-Application
rather	O
than	O
every	O
time	O
the	O
program	O
is	O
run	O
—	O
for	O
instance	O
,	O
by	O
having	O
the	O
compiler	B-Language
unroll	B-Operating_System
loops	I-Operating_System
to	O
eliminate	O
jumps	O
and	O
loop	O
count	O
decrements	O
whenever	O
the	O
program	O
is	O
executed	O
.	O
</s>
<s>
What	O
exactly	O
"	O
programming	O
at	O
compile-time	B-Application
"	O
means	O
can	O
be	O
illustrated	O
with	O
an	O
example	O
of	O
a	O
factorial	O
function	O
,	O
which	O
in	O
non-template	O
C++	B-Language
can	O
be	O
written	O
using	O
recursion	O
as	O
follows	O
:	O
</s>
<s>
By	O
using	O
template	B-Language
metaprogramming	I-Language
and	O
template	O
specialization	O
to	O
provide	O
the	O
ending	O
condition	O
for	O
the	O
recursion	O
,	O
the	O
factorials	O
used	O
in	O
the	O
program	O
—	O
ignoring	O
any	O
factorial	O
not	O
used	O
—	O
can	O
be	O
calculated	O
at	O
compile	B-Application
time	I-Application
by	O
this	O
code	O
:	O
</s>
<s>
The	O
code	O
above	O
calculates	O
the	O
factorial	O
value	O
of	O
the	O
literals	O
0	O
and	O
4	O
at	O
compile	B-Application
time	I-Application
and	O
uses	O
the	O
results	O
as	O
if	O
they	O
were	O
precalculated	O
constants	O
.	O
</s>
<s>
To	O
be	O
able	O
to	O
use	O
templates	B-Language
in	O
this	O
manner	O
,	O
the	O
compiler	B-Language
must	O
know	O
the	O
value	O
of	O
its	O
parameters	O
at	O
compile	B-Application
time	I-Application
,	O
which	O
has	O
the	O
natural	O
precondition	O
that	O
factorialxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1::value	O
can	O
only	O
be	O
used	O
if	O
X	O
is	O
known	O
at	O
compile	B-Application
time	I-Application
.	O
</s>
<s>
In	O
C++11	B-Language
and	O
C++20	B-Language
,	O
constexpr	O
and	O
consteval	O
were	O
introduced	O
to	O
let	O
the	O
compiler	B-Language
execute	O
code	O
.	O
</s>
<s>
The	O
factorial	O
example	O
above	O
is	O
one	O
example	O
of	O
compile-time	B-Application
code	O
optimization	O
in	O
that	O
all	O
factorials	O
used	O
by	O
the	O
program	O
are	O
pre-compiled	O
and	O
injected	O
as	O
numeric	O
constants	O
at	O
compilation	B-Language
,	O
saving	O
both	O
run-time	O
overhead	O
and	O
memory	O
footprint	O
.	O
</s>
<s>
As	O
another	O
,	O
more	O
significant	O
,	O
example	O
of	O
compile-time	B-Application
loop	B-Operating_System
unrolling	I-Operating_System
,	O
template	B-Language
metaprogramming	I-Language
can	O
be	O
used	O
to	O
create	O
length-n	O
vector	O
classes	O
(	O
where	O
n	O
is	O
known	O
at	O
compile	B-Application
time	I-Application
)	O
.	O
</s>
<s>
When	O
the	O
compiler	B-Language
instantiates	O
the	O
function	O
template	O
defined	O
above	O
,	O
the	O
following	O
code	O
may	O
be	O
produced	O
:	O
</s>
<s>
The	O
compiler	B-Language
's	O
optimizer	O
should	O
be	O
able	O
to	O
unroll	O
the	O
for	O
loop	O
because	O
the	O
template	O
parameter	O
length	O
is	O
a	O
constant	O
at	O
compile	B-Application
time	I-Application
.	O
</s>
<s>
This	O
dynamically	O
polymorphic	B-Application
behaviour	O
is	O
(	O
typically	O
)	O
obtained	O
by	O
the	O
creation	O
of	O
virtual	O
look-up	B-Data_Structure
tables	I-Data_Structure
for	O
classes	O
with	O
virtual	O
methods	O
,	O
tables	O
that	O
are	O
traversed	O
at	O
run	O
time	O
to	O
identify	O
the	O
method	O
to	O
be	O
invoked	O
.	O
</s>
<s>
Thus	O
,	O
run-time	O
polymorphism	B-Application
necessarily	O
entails	O
execution	O
overhead	O
(	O
though	O
on	O
modern	O
architectures	O
the	O
overhead	O
is	O
small	O
)	O
.	O
</s>
<s>
However	O
,	O
in	O
many	O
cases	O
the	O
polymorphic	B-Application
behaviour	O
needed	O
is	O
invariant	O
and	O
can	O
be	O
determined	O
at	O
compile	B-Application
time	I-Application
.	O
</s>
<s>
Then	O
the	O
Curiously	B-Language
Recurring	I-Language
Template	I-Language
Pattern	I-Language
(	O
CRTP	O
)	O
can	O
be	O
used	O
to	O
achieve	O
static	O
polymorphism	B-Application
,	O
which	O
is	O
an	O
imitation	O
of	O
polymorphism	B-Application
in	O
programming	O
code	O
but	O
which	O
is	O
resolved	O
at	O
compile	B-Application
time	I-Application
and	O
thus	O
does	O
away	O
with	O
run-time	O
virtual-table	O
lookups	O
.	O
</s>
<s>
Here	O
the	O
base	O
class	O
template	O
will	O
take	O
advantage	O
of	O
the	O
fact	O
that	O
member	O
function	O
bodies	O
are	O
not	O
instantiated	O
until	O
after	O
their	O
declarations	O
,	O
and	O
it	O
will	O
use	O
members	O
of	O
the	O
derived	O
class	O
within	O
its	O
own	O
member	O
functions	O
,	O
via	O
the	O
use	O
of	O
a	O
static_cast	O
,	O
thus	O
at	O
compilation	B-Language
generating	O
an	O
object	O
composition	O
with	O
polymorphic	B-Application
characteristics	O
.	O
</s>
<s>
As	O
an	O
example	O
of	O
real-world	O
usage	O
,	O
the	O
CRTP	O
is	O
used	O
in	O
the	O
Boost	B-Language
iterator	O
library	O
.	O
</s>
<s>
Another	O
similar	O
use	O
is	O
the	O
"	O
Barton	B-Language
–	I-Language
Nackman	I-Language
trick	I-Language
"	O
,	O
sometimes	O
referred	O
to	O
as	O
"	O
restricted	O
template	O
expansion	O
"	O
,	O
where	O
common	O
functionality	O
can	O
be	O
placed	O
in	O
a	O
base	O
class	O
that	O
is	O
used	O
not	O
as	O
a	O
contract	O
but	O
as	O
a	O
necessary	O
component	O
to	O
enforce	O
conformant	O
behaviour	O
while	O
minimising	O
code	O
redundancy	O
.	O
</s>
<s>
The	O
benefit	O
of	O
static	O
tables	O
is	O
the	O
replacement	O
of	O
"	O
expensive	O
"	O
calculations	O
with	O
a	O
simple	O
array	O
indexing	O
operation	O
(	O
for	O
examples	O
,	O
see	O
lookup	B-Data_Structure
table	I-Data_Structure
)	O
.	O
</s>
<s>
In	O
C++	B-Language
,	O
there	O
exists	O
more	O
than	O
one	O
way	O
to	O
generate	O
a	O
static	O
table	O
at	O
compile	B-Application
time	I-Application
.	O
</s>
<s>
The	O
following	O
listing	O
shows	O
an	O
example	O
of	O
creating	O
a	O
very	O
simple	O
table	O
by	O
using	O
recursive	O
structs	O
and	O
variadic	B-Language
templates	I-Language
.	O
</s>
<s>
The	O
compiler	B-Language
will	O
produce	O
code	O
similar	O
to	O
the	O
following	O
(	O
taken	O
from	O
clang	O
called	O
with	O
-Xclang	O
-ast-print	O
-fsyntax-only	O
)	O
.	O
</s>
<s>
The	O
C++20	B-Language
standard	O
brought	O
C++	B-Language
programmers	O
a	O
new	O
tool	O
for	O
meta	O
template	O
programming	O
,	O
concepts	B-Language
.	O
</s>
<s>
Concepts	B-Language
allow	O
programmers	O
to	O
specify	O
requirements	O
for	O
the	O
type	O
,	O
to	O
make	O
instantiation	B-Language
of	I-Language
template	I-Language
possible	O
.	O
</s>
<s>
The	O
compiler	B-Language
looks	O
for	O
a	O
template	O
with	O
the	O
concept	O
that	O
has	O
the	O
highest	O
requirements	O
.	O
</s>
<s>
Here	O
is	O
an	O
example	O
of	O
the	O
famous	O
Fizz	O
buzz	O
problem	O
solved	O
with	O
Template	O
Meta	B-Application
Programming	I-Application
.	O
</s>
<s>
Compile-time	B-Application
versus	O
execution-time	O
tradeoff	O
If	O
a	O
great	O
deal	O
of	O
template	B-Language
metaprogramming	I-Language
is	O
used	O
.	O
</s>
<s>
Generic	B-Language
programming	I-Language
Template	B-Language
metaprogramming	I-Language
allows	O
the	O
programmer	O
to	O
focus	O
on	O
architecture	O
and	O
delegate	O
to	O
the	O
compiler	B-Language
the	O
generation	O
of	O
any	O
implementation	O
required	O
by	O
client	O
code	O
.	O
</s>
<s>
Thus	O
,	O
template	B-Language
metaprogramming	I-Language
can	O
accomplish	O
truly	O
generic	O
code	O
,	O
facilitating	O
code	O
minimization	O
and	O
better	O
maintainability	O
.	O
</s>
<s>
Readability	O
With	O
respect	O
to	O
C++	B-Language
prior	O
to	O
C++11	B-Language
,	O
the	O
syntax	O
and	O
idioms	O
of	O
template	B-Language
metaprogramming	I-Language
were	O
esoteric	O
compared	O
to	O
conventional	O
C++	B-Language
programming	O
,	O
and	O
template	O
metaprograms	O
could	O
be	O
very	O
difficult	O
to	O
understand	O
.	O
</s>
<s>
But	O
from	O
C++11	B-Language
onward	O
the	O
syntax	O
for	O
value	O
computation	O
metaprogramming	B-Application
becomes	O
more	O
and	O
more	O
akin	O
to	O
"	O
normal	O
"	O
C++	B-Language
,	O
with	O
less	O
and	O
less	O
readability	O
penalty	O
.	O
</s>
