<s>
The	O
curiously	B-Language
recurring	I-Language
template	I-Language
pattern	I-Language
(	O
CRTP	O
)	O
is	O
an	O
idiom	O
,	O
originally	O
in	O
C++	B-Language
,	O
in	O
which	O
a	O
class	O
X	O
derives	O
from	O
a	O
class	O
template	B-Application
instantiation	O
using	O
X	O
itself	O
as	O
a	O
template	B-Application
argument	O
.	O
</s>
<s>
The	O
Microsoft	O
Implementation	O
of	O
CRTP	O
in	O
Active	B-Language
Template	I-Language
Library	I-Language
(	O
ATL	B-Language
)	O
was	O
independently	O
discovered	O
,	O
also	O
in	O
1995	O
,	O
by	O
Jan	O
Falkin	O
,	O
who	O
accidentally	O
derived	O
a	O
base	O
class	O
from	O
a	O
derived	O
class	O
.	O
</s>
<s>
Following	O
the	O
revelation	O
that	O
it	O
did	O
indeed	O
work	O
,	O
Christian	O
based	O
the	O
entire	O
ATL	B-Language
and	O
Windows	B-Application
Template	I-Application
Library	I-Application
(	O
WTL	B-Application
)	O
design	O
on	O
this	O
mistake	O
.	O
</s>
<s>
Some	O
use	O
cases	O
for	O
this	O
pattern	O
are	O
static	O
polymorphism	O
and	O
other	O
metaprogramming	O
techniques	O
such	O
as	O
those	O
described	O
by	O
Andrei	O
Alexandrescu	O
in	O
Modern	O
C++	B-Language
Design	O
.	O
</s>
<s>
It	O
also	O
figures	O
prominently	O
in	O
the	O
C++	B-Language
implementation	O
of	O
the	O
Data	B-Architecture
,	I-Architecture
Context	I-Architecture
,	I-Architecture
and	I-Architecture
Interaction	I-Architecture
paradigm	O
.	O
</s>
<s>
In	O
addition	O
,	O
CRTP	O
is	O
used	O
by	O
the	O
C++	B-Language
standard	O
library	O
to	O
implement	O
the	O
std::enable_shared_from_this	O
functionality	O
.	O
</s>
<s>
Typically	O
,	O
the	O
base	O
class	O
template	B-Application
will	O
take	O
advantage	O
of	O
the	O
fact	O
that	O
member	O
function	O
bodies	O
(	O
definitions	O
)	O
are	O
not	O
instantiated	O
until	O
long	O
after	O
their	O
declarations	O
,	O
and	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
cast	O
;	O
e.g.	O
</s>
<s>
This	O
technique	O
achieves	O
a	O
similar	O
effect	O
to	O
the	O
use	O
of	O
virtual	B-Application
functions	I-Application
,	O
without	O
the	O
costs	O
(	O
and	O
some	O
flexibility	O
)	O
of	O
dynamic	O
polymorphism	O
.	O
</s>
<s>
This	O
pattern	O
is	O
used	O
extensively	O
in	O
the	O
Windows	O
ATL	B-Language
and	O
WTL	B-Application
libraries	O
.	O
</s>
<s>
To	O
elaborate	O
on	O
the	O
above	O
example	O
,	O
consider	O
a	O
base	O
class	O
with	O
no	O
virtual	B-Application
functions	I-Application
.	O
</s>
<s>
This	O
effectively	O
emulates	O
the	O
virtual	B-Application
function	I-Application
call	O
system	O
at	O
compile	O
time	O
without	O
the	O
costs	O
in	O
size	O
or	O
function	O
call	O
overhead	O
(	O
VTBL	B-Language
structures	O
,	O
and	O
method	O
lookups	O
,	O
multiple-inheritance	O
VTBL	B-Language
machinery	O
)	O
at	O
the	O
disadvantage	O
of	O
not	O
being	O
able	O
to	O
make	O
this	O
choice	O
at	O
runtime	O
.	O
</s>
<s>
In	O
this	O
example	O
of	O
CRTP	O
,	O
this	O
distinction	O
of	O
classes	O
is	O
the	O
only	O
use	O
of	O
the	O
template	B-Application
parameter	O
(	O
T	O
in	O
counterxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1	O
)	O
and	O
the	O
reason	O
why	O
we	O
cannot	O
use	O
a	O
simple	O
un-templated	O
base	O
class	O
.	O
</s>
<s>
Method	B-Language
chaining	I-Language
,	O
also	O
known	O
as	O
named	O
parameter	O
idiom	O
,	O
is	O
a	O
common	O
syntax	O
for	O
invoking	O
multiple	O
method	O
calls	O
in	O
object-oriented	O
programming	O
languages	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
container	O
defined	O
as	O
std::vectorxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1	O
does	O
not	O
work	O
because	O
Shape	O
is	O
not	O
a	O
class	O
,	O
but	O
a	O
template	B-Application
needing	O
specialization	O
.	O
</s>
