<s>
Modern	O
C++	B-Language
Design	O
:	O
Generic	O
Programming	O
and	O
Design	O
Patterns	O
Applied	O
is	O
a	O
book	O
written	O
by	O
Andrei	O
Alexandrescu	O
,	O
published	O
in	O
2001	O
by	O
Addison-Wesley	O
.	O
</s>
<s>
It	O
has	O
been	O
regarded	O
as	O
"	O
one	O
of	O
the	O
most	O
important	O
C++	B-Language
books	O
"	O
by	O
Scott	O
Meyers	O
.	O
</s>
<s>
The	O
book	O
makes	O
use	O
of	O
and	O
explores	O
a	O
C++	B-Language
programming	O
technique	O
called	O
template	B-Language
metaprogramming	I-Language
.	O
</s>
<s>
His	O
book	O
contains	O
solutions	O
to	O
practical	O
problems	O
which	O
C++	B-Language
programmers	O
may	O
face	O
.	O
</s>
<s>
Several	O
phrases	O
from	O
the	O
book	O
are	O
now	O
used	O
within	O
the	O
C++	B-Language
community	O
as	O
generic	O
terms	O
:	O
modern	O
C++	B-Language
(	O
as	O
opposed	O
to	O
C/C	O
++	O
style	O
)	O
,	O
policy-based	O
design	O
and	O
typelist	O
.	O
</s>
<s>
All	O
of	O
the	O
code	O
described	O
in	O
the	O
book	O
is	O
freely	O
available	O
in	O
his	O
library	B-Library
Loki	B-Language
.	O
</s>
<s>
Policy-based	O
design	O
,	O
also	O
known	O
as	O
policy-based	O
class	O
design	O
or	O
policy-based	O
programming	O
,	O
is	O
the	O
term	O
used	O
in	O
Modern	O
C++	B-Language
Design	O
for	O
a	O
design	O
approach	O
based	O
on	O
an	O
idiom	O
for	O
C++	B-Language
known	O
as	O
policies	O
.	O
</s>
<s>
It	O
has	O
been	O
described	O
as	O
a	O
compile-time	B-Application
variant	O
of	O
the	O
strategy	O
pattern	O
,	O
and	O
has	O
connections	O
with	O
C++	B-Application
template	I-Application
metaprogramming	O
.	O
</s>
<s>
It	O
was	O
first	O
popularized	O
in	O
C++	B-Language
by	O
Andrei	O
Alexandrescu	O
with	O
Modern	O
C++	B-Language
Design	O
and	O
with	O
his	O
column	O
Genericxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1	O
in	O
the	O
C/C	O
++	O
Users	O
Journal	O
,	O
and	O
it	O
is	O
currently	O
closely	O
associated	O
with	O
C++	B-Language
and	O
D	B-Application
as	O
it	O
requires	O
a	O
compiler	B-Language
with	O
highly	O
robust	B-Application
support	O
for	O
templates	B-Application
,	O
which	O
was	O
not	O
common	O
before	O
about	O
2003	O
.	O
</s>
<s>
Previous	O
examples	O
of	O
this	O
design	O
approach	O
,	O
based	O
on	O
parameterized	O
generic	O
code	O
,	O
include	O
parametric	O
modules	B-Architecture
(	O
functors	B-Language
)	O
of	O
the	O
ML	B-Language
languages	I-Language
,	O
and	O
C++	B-Application
allocators	I-Application
for	O
memory	O
management	O
policy	O
.	O
</s>
<s>
The	O
central	O
idiom	O
in	O
policy-based	O
design	O
is	O
a	O
class	O
template	B-Application
(	O
called	O
the	O
host	O
class	O
)	O
,	O
taking	O
several	O
type	O
parameters	O
as	O
input	O
,	O
which	O
are	O
instantiated	O
with	O
types	O
selected	O
by	O
the	O
user	O
(	O
called	O
policy	O
classes	O
)	O
,	O
each	O
implementing	O
a	O
particular	O
implicit	O
interface	B-Application
(	O
called	O
a	O
policy	O
)	O
,	O
and	O
encapsulating	O
some	O
orthogonal	O
(	O
or	O
mostly	O
orthogonal	O
)	O
aspect	O
of	O
the	O
behavior	O
of	O
the	O
instantiated	O
host	O
class	O
.	O
</s>
<s>
By	O
supplying	O
a	O
host	O
class	O
combined	O
with	O
a	O
set	O
of	O
different	O
,	O
canned	O
implementations	O
for	O
each	O
policy	O
,	O
a	O
library	B-Library
or	O
module	B-Architecture
can	O
support	O
an	O
exponential	O
number	O
of	O
different	O
behavior	O
combinations	O
,	O
resolved	O
at	O
compile	B-Application
time	I-Application
,	O
and	O
selected	O
by	O
mixing	O
and	O
matching	O
the	O
different	O
supplied	O
policy	O
classes	O
in	O
the	O
instantiation	O
of	O
the	O
host	O
class	O
template	B-Application
.	O
</s>
<s>
Additionally	O
,	O
by	O
writing	O
a	O
custom	O
implementation	O
of	O
a	O
given	O
policy	O
,	O
a	O
policy-based	O
library	B-Library
can	O
be	O
used	O
in	O
situations	O
requiring	O
behaviors	O
unforeseen	O
by	O
the	O
library	B-Library
implementor	O
.	O
</s>
<s>
Even	O
in	O
cases	O
where	O
no	O
more	O
than	O
one	O
implementation	O
of	O
each	O
policy	O
will	O
ever	O
be	O
used	O
,	O
decomposing	O
a	O
class	O
into	O
policies	O
can	O
aid	O
the	O
design	O
process	O
,	O
by	O
increasing	O
modularity	B-Architecture
and	O
highlighting	O
exactly	O
where	O
orthogonal	O
design	O
decisions	O
have	O
been	O
made	O
.	O
</s>
<s>
While	O
assembling	O
software	O
components	O
out	O
of	O
interchangeable	O
modules	B-Architecture
is	O
a	O
far	O
from	O
new	O
concept	O
,	O
policy-based	O
design	O
represents	O
an	O
innovation	O
in	O
the	O
way	O
it	O
applies	O
that	O
concept	O
at	O
the	O
(	O
relatively	O
low	O
)	O
level	O
of	O
defining	O
the	O
behavior	O
of	O
an	O
individual	O
class	O
.	O
</s>
<s>
Policy	O
classes	O
have	O
some	O
similarity	O
to	O
callbacks	O
,	O
but	O
differ	O
in	O
that	O
,	O
rather	O
than	O
consisting	O
of	O
a	O
single	O
function	O
,	O
a	O
policy	O
class	O
will	O
typically	O
contain	O
several	O
related	O
functions	O
(	O
methods	O
)	O
,	O
often	O
combined	O
with	O
state	B-Application
variables	O
or	O
other	O
facilities	O
such	O
as	O
nested	O
types	O
.	O
</s>
<s>
A	O
policy-based	O
host	O
class	O
can	O
be	O
thought	O
of	O
as	O
a	O
type	O
of	O
metafunction	B-Language
,	O
taking	O
a	O
set	O
of	O
behaviors	O
represented	O
by	O
types	O
as	O
input	O
,	O
and	O
returning	O
as	O
output	O
a	O
type	O
representing	O
the	O
result	O
of	O
combining	O
those	O
behaviors	O
into	O
a	O
functioning	O
whole	O
.	O
</s>
<s>
(	O
Unlike	O
MPL	B-Language
metafunctions	O
,	O
however	O
,	O
the	O
output	O
is	O
usually	O
represented	O
by	O
the	O
instantiated	O
host	O
class	O
itself	O
,	O
rather	O
than	O
a	O
nested	O
output	O
type	O
.	O
)	O
</s>
<s>
A	O
key	O
feature	O
of	O
the	O
policy	O
idiom	O
is	O
that	O
,	O
usually	O
(	O
though	O
it	O
is	O
not	O
strictly	O
necessary	O
)	O
,	O
the	O
host	O
class	O
will	O
derive	B-Language
from	O
(	O
make	O
itself	O
a	O
child	O
class	O
of	O
)	O
each	O
of	O
its	O
policy	O
classes	O
using	O
(	O
public	O
)	O
multiple	B-Application
inheritance	I-Application
.	O
</s>
<s>
A	O
notable	O
feature	O
of	O
this	O
aspect	O
of	O
the	O
policy	O
idiom	O
is	O
that	O
,	O
relative	O
to	O
object-oriented	B-Language
programming	I-Language
,	O
policies	O
invert	O
the	O
relationship	O
between	O
base	O
class	O
and	O
derived	O
class	O
-	O
whereas	O
in	O
OOP	O
interfaces	B-Application
are	O
traditionally	O
represented	O
by	O
(	O
abstract	O
)	O
base	O
classes	O
and	O
implementations	O
of	O
interfaces	B-Application
by	O
derived	O
classes	O
,	O
in	O
policy-based	O
design	O
the	O
derived	O
(	O
host	O
)	O
class	O
represents	O
the	O
interfaces	B-Application
and	O
the	O
base	O
(	O
policy	O
)	O
classes	O
implement	O
them	O
.	O
</s>
<s>
In	O
the	O
case	O
of	O
policies	O
,	O
the	O
public	O
inheritance	B-Language
does	O
not	O
represent	O
an	O
is-a	O
relationship	O
between	O
the	O
host	O
and	O
the	O
policy	O
classes	O
.	O
</s>
<s>
A	O
disadvantage	O
of	O
policies	O
in	O
their	O
current	O
incarnation	O
is	O
that	O
the	O
policy	O
interface	B-Application
does	O
n't	O
have	O
a	O
direct	O
,	O
explicit	O
representation	O
in	O
code	O
,	O
but	O
rather	O
is	O
defined	O
implicitly	O
,	O
via	O
duck	B-Application
typing	I-Application
,	O
and	O
must	O
be	O
documented	O
separately	O
and	O
manually	O
,	O
in	O
comments	O
.	O
</s>
<s>
The	O
main	O
idea	O
is	O
to	O
use	O
commonality-variability	O
analysis	O
to	O
divide	O
the	O
type	O
into	O
the	O
fixed	O
implementation	O
and	O
interface	B-Application
,	O
the	O
policy-based	O
class	O
,	O
and	O
the	O
different	O
policies	O
.	O
</s>
<s>
Basically	O
,	O
the	O
designer	O
of	O
the	O
main	O
template	B-Application
class	O
will	O
define	O
what	O
the	O
policy	O
classes	O
should	O
provide	O
,	O
what	O
customization	O
points	O
they	O
need	O
to	O
implement	O
.	O
</s>
<s>
Graphic	O
designers	O
are	O
able	O
to	O
give	O
a	O
name	O
to	O
their	O
policies	O
,	O
which	O
represent	O
concepts	B-Language
,	O
and	O
not	O
those	O
which	O
represent	O
operations	O
or	O
minor	O
implementation	O
details	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
template	B-Application
method	B-Language
pattern	O
can	O
be	O
reinterpreted	O
for	O
compile	B-Application
time	I-Application
,	O
so	O
that	O
a	O
main	O
class	O
has	O
a	O
skeleton	O
algorithm	O
,	O
whichat	O
customization	O
pointscalls	O
the	O
appropriate	O
functions	O
of	O
some	O
of	O
the	O
policies	O
.	O
</s>
<s>
This	O
will	O
be	O
achieved	O
dynamically	O
by	O
concepts	B-Language
in	O
future	O
versions	O
of	O
C++	B-Language
.	O
</s>
<s>
Presented	O
below	O
is	O
a	O
simple	O
(	O
contrived	O
)	O
example	O
of	O
a	O
C++	B-Language
hello	O
world	O
program	O
,	O
where	O
the	O
text	O
to	O
be	O
printed	O
and	O
the	O
method	B-Language
of	O
printing	O
it	O
are	O
decomposed	O
using	O
policies	O
.	O
</s>
<s>
Note	O
that	O
the	O
generic	O
implementation	O
is	O
in	O
Run	O
and	O
therefore	O
the	O
code	O
is	O
unable	O
to	O
be	O
compiled	B-Language
unless	O
both	O
policies	O
(	O
Print	O
and	O
Message	O
)	O
are	O
provided	O
.	O
</s>
<s>
Loki	B-Language
is	O
the	O
name	O
of	O
a	O
C++	B-Language
software	B-Library
library	I-Library
written	O
by	O
Andrei	O
Alexandrescu	O
as	O
part	O
of	O
his	O
book	O
Modern	O
C++	B-Language
Design	O
.	O
</s>
<s>
The	O
library	B-Library
makes	O
extensive	O
use	O
of	O
C++	B-Application
template	I-Application
metaprogramming	O
and	O
implements	O
several	O
commonly	O
used	O
tools	O
:	O
typelist	O
,	O
functor	B-Language
,	O
singleton	O
,	O
smart	B-Language
pointer	I-Language
,	O
object	B-Application
factory	I-Application
,	O
visitor	B-Language
and	O
multimethods	O
.	O
</s>
<s>
Originally	O
the	O
library	B-Library
was	O
only	O
compatible	O
with	O
two	O
of	O
the	O
most	O
standard	O
conforming	O
C++	B-Language
compilers	B-Language
(	O
CodeWarrior	B-Operating_System
and	O
Comeau	B-General_Concept
C/C	I-General_Concept
++	I-General_Concept
)	O
:	O
later	O
efforts	O
have	O
made	O
it	O
usable	O
with	O
a	O
wide	O
array	O
of	O
compilers	B-Language
(	O
including	O
older	O
Visual	B-Application
C++	I-Application
6.0	I-Application
,	O
Borland	B-Language
C++	I-Language
Builder	I-Language
6.0	I-Language
,	O
Clang	B-Application
and	O
GCC	B-Application
)	O
.	O
</s>
<s>
Compiler	B-Language
vendors	O
used	O
Loki	B-Language
as	O
a	O
compatibility	O
benchmark	O
,	O
further	O
increasing	O
the	O
number	O
of	O
compliant	O
compilers	B-Language
.	O
</s>
<s>
Maintenance	O
and	O
further	O
development	O
of	O
Loki	B-Language
has	O
been	O
continued	O
through	O
an	O
open-source	O
community	O
led	O
by	O
Peter	O
Kümmel	O
and	O
Richard	O
Sposato	O
as	O
a	O
.	O
</s>
<s>
Ongoing	O
contributions	O
by	O
many	O
people	O
have	O
improved	O
the	O
overall	O
robustness	B-Application
and	O
functionality	O
of	O
the	O
library	B-Library
.	O
</s>
<s>
Loki	B-Language
is	O
not	O
tied	O
to	O
the	O
book	O
anymore	O
as	O
it	O
already	O
has	O
a	O
lot	O
of	O
new	O
components	O
(	O
e.g.	O
</s>
<s>
Loki	B-Language
inspired	O
similar	O
tools	O
and	O
functionality	O
now	O
also	O
present	O
in	O
the	O
Boost	B-Language
library	I-Language
collection	O
.	O
</s>
