8 Star 183 Fork 8

Lucifer / Programming-in-D-in-Chinese

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
templatesmore.html 377.33 KB
一键复制 编辑 原始数据 按行查看 历史
Lucifer 提交于 2014-02-06 17:05 . Programming in D in English
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>More Templates</title>
</head>
<body>
<h4 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.5em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
More Templates</h4>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
We have seen the power and convenience of templates in<span class="Apple-converted-space">&nbsp;</span><a href="http://ddili.org/ders/d.en/templates.html" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: purple; text-decoration: none; background-position: initial initial; background-repeat: initial initial;">the
Templates chapter</a>. A single templated definition of an algorithm or a data
structure is sufficient to use that definition for multiple types.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
That chapter covered only the most common uses of templates: function,<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">struct</code>,
and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">class</code><span class="Apple-converted-space">&nbsp;</span>templates
and their uses with<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">type</i><span class="Apple-converted-space">&nbsp;</span>template
parameters. In this chapter we will see templates in more detail. Before going
further, I recommend that you review at least the summary section of that
chapter.</p>
<h5 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.25em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The shortcut syntax</h5>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
In addition to being powerful, D templates are easy to define and use and they
are very readable. Defining a function,<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">struct</code>,
or<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">class</code>template
is as simple as providing a template parameter list:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">T twice<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">(T)</span>(T value)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">return</span> 2 * value;
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">class</span> Fraction<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">(T)</span>
{
T numerator;
T denominator;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Template definitions like the ones above are taking advantage of D&#39;s shortcut
template syntax.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
In their full syntax, templates are defined by the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">template</code><span class="Apple-converted-space">&nbsp;</span>keyword.
The equivalents of the two template definitions above are the following:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">template</span> twice<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">(T)</span>
{
T twice(T value)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">return</span> 2 * value;
}
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">template</span> Fraction<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">(T)</span>
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">class</span> Fraction
{
T numerator;
T denominator;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span> }
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Although most templates are defined by the shortcut syntax, the compiler always
uses the full syntax. We can imagine the compiler applying the following steps
to convert a shortcut syntax to its full form behind the scenes:</p>
<ol style="margin: 1em 1em 0.75em 1.5em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Wrap the definition by a template block.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Give the same name to that block.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Move the template parameter list to the template block.</li>
</ol>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The full syntax that is arrived after those steps is called an<i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">eponymous
template</i>, which the programmer can define explicitly as well. We will see
eponymous templates later below.</p>
<h6 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.1em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Template name space</h6>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
It is possible to have more than one definition inside a template block. The
following template contains both a function and a<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">struct</code>definition:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">template</span> MyTemplate(T)
{
T foo(T value)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">return</span> value / 3;
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">struct</span> S
{
T member;
}
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Instantiating the template for a specific type instantiates all of the
definitions inside the block. The following code instantiates the template for<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">int</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">double</code>:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">auto</span> result = <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">MyTemplate!<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span></span>.foo(42);
writeln(result);
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">auto</span> s = <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">MyTemplate!<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">double</span></span>.S(5.6);
writeln(s.member);
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
A specific instantiation of a template introduces a<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">name
space</i>. The definitions that are inside an instantiation can be used by that
name. However, if these names are too long, it is always possible to use aliases
as we have seen in<span class="Apple-converted-space">&nbsp;</span><a href="http://ddili.org/ders/d.en/alias.html" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: purple; text-decoration: none; background-position: initial initial; background-repeat: initial initial;">the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: purple; background-position: initial initial; background-repeat: initial initial;">alias</code><span class="Apple-converted-space">&nbsp;</span>chapter</a>:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">alias</span> MyStruct = MyTemplate!<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">dchar</span>.S;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">auto</span> o = <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">MyStruct</span>(&#39;a&#39;);
writeln(o.member);
</pre>
<h6 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.1em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Eponymous templates</h6>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Eponymous templates are<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">template</code><span class="Apple-converted-space">&nbsp;</span>blocks
that contain a definition that has the same name as that block. If fact, each
shortcut template syntax is the shortcut of an eponymous template.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
As an example, imagine that a program needs to qualify types that are larger
than 20 bytes as<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">too
large</i>. Such a qualification can be achieved by a constant<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">bool</code><span class="Apple-converted-space">&nbsp;</span>value
inside a template block:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">template</span> isTooLarge(T)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">enum</span> isTooLarge = T.sizeof &gt; 20;
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Note how the names of both the template block and its only definition are the
same. This eponymous template is used by the shortcut syntax instead of the full<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">isTooLarge!int.isTooLarge</code>:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"> writeln(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">isTooLarge!<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span></span>);
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The highlighted part above is the same as the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">bool</code><span class="Apple-converted-space">&nbsp;</span>value
inside the block. Since the size of<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">int</code><span class="Apple-converted-space">&nbsp;</span>is
less than 20, the output of the code would be<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">false</code>.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
That eponymous template can be defined by the shortcut syntax as well:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">enum</span> isTooLarge<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">(T)</span> = T.sizeof &gt; 20;
</pre>
<h5 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.25em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Template types</h5>
<h6 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.1em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Function, class, and struct templates</h6>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
We have already covered function,<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">class</code>,
and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">struct</code><span class="Apple-converted-space">&nbsp;</span>templates
in<span class="Apple-converted-space">&nbsp;</span><a href="http://ddili.org/ders/d.en/templates.html" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: purple; text-decoration: none; background-position: initial initial; background-repeat: initial initial;">the
Templates chapter</a><span class="Apple-converted-space">&nbsp;</span>and we have
seen many examples of them since then.</p>
<h6 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.1em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Union templates</h6>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Union templates are similar to struct templates. The shortcut syntax is
available for them as well.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
As an example, let&#39;s design a more general version of the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">IpAdressunion</code><span class="Apple-converted-space">&nbsp;</span>that
we have seen in<span class="Apple-converted-space">&nbsp;</span><a href="http://ddili.org/ders/d.en/union.html" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: purple; text-decoration: none; background-position: initial initial; background-repeat: initial initial;">the
Unions chapter</a>. There, the value of the IPv4 address was kept as a<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">uint</code><span class="Apple-converted-space">&nbsp;</span>member
in that earlier version of<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">IpAdress</code>,
and the element type of the segment array was<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">ubyte</code>:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">union</span> IpAddress
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">uint</span> value;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">ubyte</span>[4] bytes;
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">bytes</code><span class="Apple-converted-space">&nbsp;</span>array
was an easy access to the four segments of the IPv4 address.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The same concept can be implemented in a more general way as the following<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">union</code><span class="Apple-converted-space">&nbsp;</span>template:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">union</span> SegmentedValue(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">ActualT, SegmentT</span>)
{
ActualT value;
SegmentT[<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">/* number of segments */</span>] segments;
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
That template would allow specifying the types of the value and its segments
freely.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The number of segments that are needed depends on the types of the actual value
and the segments. Since an IPv4 address has four<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">ubyte</code>segments,
that value was hard-coded as<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">4</code><span class="Apple-converted-space">&nbsp;</span>in
the earlier definition of<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">IpAddress</code>.
For the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">SegmentedValue</code><span class="Apple-converted-space">&nbsp;</span>template,
the number of segments must be computed at compile time when the template is
instantiated for the two specific types.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The following eponymous template takes advantage of the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">.sizeof</code>properties
of the two types to calculate the number of segments needed:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">template</span> segmentCount(ActualT, SegmentT)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">enum</span> segmentCount = ((ActualT.sizeof + (SegmentT.sizeof - 1))
/ SegmentT.sizeof);
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The shortcut syntax may be more readable:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">enum</span> segmentCount(ActualT, SegmentT) =
((ActualT.sizeof + (SegmentT.sizeof - 1))
/ SegmentT.sizeof);
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
<b style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Note:</b><span class="Apple-converted-space">&nbsp;</span>The expression<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">SegmentT.sizeof
- 1</code><span class="Apple-converted-space">&nbsp;</span>is for when the sizes of
the types cannot be divided evenly. For example, when the actual type is 5 bytes
and the segment type is 2 bytes, even though a total of 3 segments are needed,
the result of the integer division 5/2 would incorrectly be 2.</i></p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The definition of the union template is now complete:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">union</span> SegmentedValue(ActualT, SegmentT)
{
ActualT value;
SegmentT[segmentCount!(ActualT, SegmentT)] segments;
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Instantiation of the template for<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">uint</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">ubyte</code><span class="Apple-converted-space">&nbsp;</span>would
be the equivalent of the earlier definition of<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">IpAddress</code>:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">import</span> std.stdio;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> main()
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">auto</span> address = SegmentedValue!(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">uint</span>, <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">ubyte</span></span>)(0xc0a80102);
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">foreach</span> (octet; address.segments) {
write(octet, &#39; &#39;);
}
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The output of the program is the same as the one in<span class="Apple-converted-space">&nbsp;</span><a href="http://ddili.org/ders/d.en/union.html" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: purple; text-decoration: none; background-position: initial initial; background-repeat: initial initial;">the
Unions chapter</a>:</p>
<pre class="shell" style="margin: 0.5em 1em 1em; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(224, 224, 224); font-weight: bold; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">2 1 168 192
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
To demonstrate the flexibility of this template, let&#39;s imagine that it is
required to access the parts of the IPv4 address as two<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">ushort</code>values.
It would be as easy as providing<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">ushort</code><span class="Apple-converted-space">&nbsp;</span>as
the segment type:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">auto</span> address = SegmentedValue!(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">uint</span>, <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">ushort</span></span>)(0xc0a80102);
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Although unusual for an IPv4 address, the output of the program would consist of
two<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">ushort</code><span class="Apple-converted-space">&nbsp;</span>segment
values:</p>
<pre class="shell" style="margin: 0.5em 1em 1em; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(224, 224, 224); font-weight: bold; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">258 49320
</pre>
<h6 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.1em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Interface templates</h6>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Interface templates provide flexibility on the types that are used on an
interface (as well as values such as sizes of fixed-length arrays and other
features of an interface).</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Let&#39;s define an interface for colored objects where the type of the color is
determined by a template parameter:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">interface</span> ColoredObject(ColorT)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> paint(ColorT color);
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
That interface template requires that its subtypes must define the<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">paint()</code><span class="Apple-converted-space">&nbsp;</span>function
but it leaves the type of the color flexible.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
A class that represents a frame on a web page may choose to use a color type
that is represented by its red, green, and blue components:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">struct</span> RGB
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">ubyte</span> red;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">ubyte</span> green;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">ubyte</span> blue;
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">class</span> PageFrame : ColoredObject<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">!RGB</span>
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> paint(RGB color)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span> }
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
On the other hand, a class that uses the frequency of light can choose a
completely different type to represent color:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">alias</span> Frequency = <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">double</span>;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">class</span> Bulb : ColoredObject<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">!Frequency</span>
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> paint(Frequency color)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span> }
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
However, as explained in<span class="Apple-converted-space">&nbsp;</span><a href="http://ddili.org/ders/d.en/templates.html" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: purple; text-decoration: none; background-position: initial initial; background-repeat: initial initial;">the
Templates chapter</a>, &quot;every different instantiation of a template is a
different type&quot;. Accordingly, the interfaces<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">ColoredObject!RGB</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">ColoredObject!Frequency</code><span class="Apple-converted-space">&nbsp;</span>are
unrelated interfaces, and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">PageFrame</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">Bulb</code><span class="Apple-converted-space">&nbsp;</span>are
unrelated classes.</p>
<h5 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.25em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Kinds of template parameters</h5>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The template parameters that we have seen so far have all been<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">type</i>parameters.
So far, parameters like<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">T</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">ColorT</code><span class="Apple-converted-space">&nbsp;</span>all
represented types. For example,<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">T</code><span class="Apple-converted-space">&nbsp;</span>meant<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">int</code>,<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">double</code>,<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">Student</code>,
etc. depending on the instantiation of the template.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
There are other kinds of template parameters: value,<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">this</code>,<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">alias</code>,
and tuple.</p>
<h6 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.1em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Type template parameters</h6>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
This section is only for completeness. All of the templates that we seen so far
had type parameters.</p>
<h6 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.1em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Value template parameters</h6>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Value template parameters allow flexibility on certain values used in the
template implementation.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Since templates are a compile-time feature, the values for the value template
parameters must be known at compile time; values that must be calculated at run
time cannot be used.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
To see the advantage of value template parameters, let&#39;s start with a set of
structs representing geometric shapes:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">struct</span> Triangle
{
Point[3] corners;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">struct</span> Rectangle
{
Point[4] corners;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">struct</span> Pentagon
{
Point[5] corners;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Let&#39;s assume that other member variables and member functions of those types are
exactly the same and that the only difference is the<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">value</i>that
determines the number of corners.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Value template parameters help in such cases. The following struct template is
sufficient to represent all of the types above and more:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">struct</span> Polygon<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">(size_t N)</span>
{
Point[N] corners;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The only template parameter of that struct template is a value named<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">N</code>of
type<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">size_t</code>.
The value<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">N</code><span class="Apple-converted-space">&nbsp;</span>can
be used as a compile-time constant anywhere inside the template.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
That template is flexible enough to represent shapes of any sides:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">auto</span> centagon = Polygon!100();
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The following aliases correspond to the earlier struct definitions:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">alias</span> Triangle = Polygon!3;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">alias</span> Rectangle = Polygon!4;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">alias</span> Pentagon = Polygon!5;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">auto</span> triangle = Triangle();
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">auto</span> rectangle = Rectangle();
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">auto</span> pentagon = Pentagon();
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
As long as the value is known at compile time, a value template parameter can be
of any arithmetic or string type.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The following example uses a<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">string</code><span class="Apple-converted-space">&nbsp;</span>template
parameter to represent an XML tag to produce a simple XML output:</p>
<ul style="margin: 0.5em 1em 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
First the tag between the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">&lt;</code>&nbsp;<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">&gt;</code><span class="Apple-converted-space">&nbsp;</span>characters:<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">&lt;tag&gt;</code></li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Then the value</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Finally the tag between the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">&lt;/</code>&nbsp;<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">&gt;</code><span class="Apple-converted-space">&nbsp;</span>characters:<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">&lt;/tag&gt;</code></li>
</ul>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
For example, an XML tag representing<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">location
42</i><span class="Apple-converted-space">&nbsp;</span>would be printed as<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">&lt;location&gt;42&lt;/location&gt;</code>.</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">class</span> XmlElement<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">(string tag)</span>
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">double</span> value;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">this</span>(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">double</span> value)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">this</span>.value = value;
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">override</span> string toString() <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">const</span>
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">return</span> format(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;&lt;%s&gt;%s&lt;/%s&gt;&quot;</span>, tag, value, tag);
}
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Note that the template parameter is not about a type that is used in the
implementation of the template, rather it is about a<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">string</code><span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">value</i>.
That value can be used anywhere inside the template as a<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">string</code>.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The XML elements that a program needs can be defined as aliases as in the
following code:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">alias</span> Location = XmlElement!<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;location&quot;</span>;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">alias</span> Temperature = XmlElement!<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;temperature&quot;</span>;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">alias</span> Weight = XmlElement!<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;weight&quot;</span>;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> main()
{
Object[] elements;
elements ~= <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">new</span> Location(1);
elements ~= <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">new</span> Temperature(23);
elements ~= <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">new</span> Weight(78);
writeln(elements);
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The output:</p>
<pre class="shell" style="margin: 0.5em 1em 1em; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(224, 224, 224); font-weight: bold; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">[&lt;location&gt;1&lt;/location&gt;, &lt;temperature&gt;23&lt;/temperature&gt;, &lt;weight&gt;78&lt;/weight&gt;]
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Value template parameters can have default values as well. For example, the
following struct template represents points in a multi-dimensional space where
the default number of dimensions is 3:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">struct</span> Point(T, size_t dimension <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">= 3</span>)
{
T[dimension] coordinates;
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
That template can be used without specifying the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">dimension</code><span class="Apple-converted-space">&nbsp;</span>template
parameter:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"> Point!<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">double</span> center; <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// a point in 3-dimensional space
</span></pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The number of dimensions can still be specified when needed:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"> Point!(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span>, 2) point; <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// a point on a surface
</span></pre>
<h6 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.1em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">
this</code><span class="Apple-converted-space">&nbsp;</span>template parameters for
member function templates</h6>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Member functions can be templates as well. Their template parameters have the
same meanings as other templates.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
However, unlike other templates, member function template parameters can also be<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;"><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">this</code><span class="Apple-converted-space">&nbsp;</span>parameters</i>.
In that case, the identifier that comes after the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">this</code><span class="Apple-converted-space">&nbsp;</span>keyword
represents the exact type of the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">this</code><span class="Apple-converted-space">&nbsp;</span>reference
of the object. (<i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;"><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">this</code><span class="Apple-converted-space">&nbsp;</span>reference</i><span class="Apple-converted-space">&nbsp;</span>means
the object itself, as is commonly written in constructors as<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">this.member&nbsp;=&nbsp;value</code>.)</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">struct</span> MyStruct(T)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> foo(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">this</span> OwnType</span>)() <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">const</span>
{
writeln(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;Type of this object: &quot;</span>, OwnType.stringof);
}
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">OwnType</code><span class="Apple-converted-space">&nbsp;</span>template
parameter is the actual type of the object that the member function is called
on:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">auto</span> m = MyStruct!<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span>();
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">auto</span> c = <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">const</span>(MyStruct!<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span>)();
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">auto</span> i = <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">immutable</span>(MyStruct!<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span>)();
m.foo();
c.foo();
i.foo();
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The output:</p>
<pre class="shell" style="margin: 0.5em 1em 1em; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(224, 224, 224); font-weight: bold; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">Type of this object: MyStruct!int
Type of this object: const(MyStruct!int)
Type of this object: immutable(MyStruct!int)
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
As you can see, the type includes the corresponding type of<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">T</code><span class="Apple-converted-space">&nbsp;</span>as
well as the type qualifiers like<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">const</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">immutable</code>.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">struct</code><span class="Apple-converted-space">&nbsp;</span>(or<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">class</code>)
need not be a template.<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">this</code><span class="Apple-converted-space">&nbsp;</span>template
parameters can appear on member function templates of non-templated types as
well.</p>
<h6 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.1em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">
alias</code><span class="Apple-converted-space">&nbsp;</span>template parameters</h6>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">
alias</code><span class="Apple-converted-space">&nbsp;</span>template parameters can
correspond to any symbol or expression that is used in the program. The only
constraint on such a template argument is that the argument must be compatible
with its use inside the template.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">
filter()</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">map()</code><span class="Apple-converted-space">&nbsp;</span>use<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">alias</code><span class="Apple-converted-space">&nbsp;</span>template
parameters to determine the operations that they execute.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Let&#39;s see a simple example on a<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">struct</code><span class="Apple-converted-space">&nbsp;</span>template
that is for modifying an existing variable. The<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">struct</code><span class="Apple-converted-space">&nbsp;</span>template
takes the variable as an<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">alias</code><span class="Apple-converted-space">&nbsp;</span>parameter:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">struct</span> MyStruct(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">alias</span> variable)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> set(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> value)
{
variable = value;
}
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The member function simply assigns its parameter to the variable that the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">struct</code><span class="Apple-converted-space">&nbsp;</span>template
is instantiated with. That variable must be specified during the instantiation
of the template:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> x = 1;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> y = 2;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">auto</span> object = MyStruct!<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">x</span>();
object.set(10);
writeln(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;x: &quot;</span>, x, <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;, y: &quot;</span>, y);
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
In that instantiation, the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">variable</code><span class="Apple-converted-space">&nbsp;</span>template
parameter corresponds to the variable<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">x</code>:</p>
<pre class="shell" style="margin: 0.5em 1em 1em; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(224, 224, 224); font-weight: bold; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">x: <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">10</span>, y: 2
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Conversely,<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">MyStruct!y</code><span class="Apple-converted-space">&nbsp;</span>instantiation
of the template would associate<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">variable</code><span class="Apple-converted-space">&nbsp;</span>with<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">y</code>.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Let&#39;s now have an<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">alias</code><span class="Apple-converted-space">&nbsp;</span>parameter
that represents a callable entity, similar to<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">filter()</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">map()</code>:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> caller(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">alias</span> func)()
{
write(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;calling: &quot;</span>);
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">func()</span>;
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
As seen by the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">()</code><span class="Apple-converted-space">&nbsp;</span>parentheses,<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">caller()</code><span class="Apple-converted-space">&nbsp;</span>uses
its template parameter as a function. Additionally, since the parentheses are
empty, it must be legal to call the function without specifying any arguments.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Let&#39;s have the following two functions that match that description. They can
both represent<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">func</code><span class="Apple-converted-space">&nbsp;</span>because
they can be called as<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">func()</code><span class="Apple-converted-space">&nbsp;</span>in
the template:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> foo()
{
writeln(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;foo called.&quot;</span>);
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> bar()
{
writeln(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;bar called.&quot;</span>);
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Those functions can be used as the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">alias</code><span class="Apple-converted-space">&nbsp;</span>parameter
of<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">caller()</code>:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"> caller!foo();
caller!bar();
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The output:</p>
<pre class="shell" style="margin: 0.5em 1em 1em; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(224, 224, 224); font-weight: bold; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">calling: foo called.
calling: bar called.
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
As long as it matches the way it is used in the template, any symbol can be used
as an<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">alias</code><span class="Apple-converted-space">&nbsp;</span>parameter.
As a counter example, using an<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">int</code>variable
with<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">caller()</code><span class="Apple-converted-space">&nbsp;</span>would
cause a compilation error:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> variable;
caller!variable(); <span class="d_comment" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ← <span class="d_hata" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 64, 64); color: rgb(0, 0, 0); background-position: initial initial; background-repeat: initial initial;">compilation ERROR</span></span>
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The compilation error indicates that the variable does not match its use in the
template:</p>
<pre class="shell" style="margin: 0.5em 1em 1em; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(224, 224, 224); font-weight: bold; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">Error: <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">function expected before ()</span>, not variable of type int
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Although the mistake is with the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">caller!variable</code><span class="Apple-converted-space">&nbsp;</span>instantiation,
the compilation error necessarily points at<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">func()</code><span class="Apple-converted-space">&nbsp;</span>inside
the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">caller()</code>template
because from the point of view of the compiler the error is with trying to call<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">variable</code><span class="Apple-converted-space">&nbsp;</span>as
a function. One way of dealing with this issue is to use<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">template
constraints</i>, which we will see below.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
If the variable supports the function call syntax perhaps because it has an<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">opCall()</code><span class="Apple-converted-space">&nbsp;</span>overload
or it is a function literal, it would still work with the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">caller()</code><span class="Apple-converted-space">&nbsp;</span>template.
The following example demonstrates both of those cases:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">class</span> C
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> opCall()
{
writeln(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;C.opCall called.&quot;</span>);
}
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">auto</span> o = <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">new</span> C();
caller!o();
caller!({ writeln(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;Function literal called.&quot;</span>); })();
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The output:</p>
<pre class="shell" style="margin: 0.5em 1em 1em; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(224, 224, 224); font-weight: bold; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">calling: C.opCall called.
calling: Function literal called.
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">
alias</code><span class="Apple-converted-space">&nbsp;</span>parameters can be
specialized as well. However, they have a different specialization syntax. The
specialized type must be specified between the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">alias</code><span class="Apple-converted-space">&nbsp;</span>keyword
and the name of the parameter:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">import</span> std.stdio;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> foo(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">alias</span> variable)()
{
writefln(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;The general definition is using &#39;%s&#39; of type %s.&quot;</span>,
variable.stringof, <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">typeof</span>(variable).stringof);
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> foo(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">alias</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span></span> i)()
{
writefln(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;The int specialization is using &#39;%s&#39;.&quot;</span>,
i.stringof);
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> foo(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">alias</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">double</span></span> d)()
{
writefln(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;The double specialization is using &#39;%s&#39;.&quot;</span>,
d.stringof);
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> main()
{
string name;
foo!name();
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> count;
foo!count();
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">double</span> length;
foo!length();
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Also note that<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">alias</code><span class="Apple-converted-space">&nbsp;</span>parameters
make the names of the actual variables available inside the template:</p>
<pre class="shell" style="margin: 0.5em 1em 1em; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(224, 224, 224); font-weight: bold; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">The general definition is using &#39;name&#39; of type string.
The int specialization is using &#39;count&#39;.
The double specialization is using &#39;length&#39;.
</pre>
<h6 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.1em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Tuple template parameters</h6>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
We have seen in<span class="Apple-converted-space">&nbsp;</span><a href="http://ddili.org/ders/d.en/parameter_flexibility.html" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: purple; text-decoration: none; background-position: initial initial; background-repeat: initial initial;">the
Variable Number of Parameters chapter</a><span class="Apple-converted-space">&nbsp;</span>that
variadic functions can take any number and any type of parameters. For example,<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">writeln()</code><span class="Apple-converted-space">&nbsp;</span>can
be called with any number of parameters of any type.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Templates can be variadic as well. A template parameter that consists of a name
followed by<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">...</code><span class="Apple-converted-space">&nbsp;</span>allows
any number and types of parameters at that parameter&#39;s position. Such parameters
appear as a tuple inside the template, which can be used like a<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">TypeTuple</code>.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Let&#39;s see an example of this with a template that simply prints information
about every template argument that it is instantiated with:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> info(T...)(T args)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The template parameter<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">T...</code><span class="Apple-converted-space">&nbsp;</span>makes<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">info</code><span class="Apple-converted-space">&nbsp;</span>a<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">variadic
template</i>. Both<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">T</code>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">args</code><span class="Apple-converted-space">&nbsp;</span>are
tuples:</p>
<ul style="margin: 0.5em 1em 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">
T</code><span class="Apple-converted-space">&nbsp;</span>represents the types of
the arguments.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">
args</code><span class="Apple-converted-space">&nbsp;</span>represents the
arguments themselves.</li>
</ul>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The following example instantiates that function template with three values of
three different types:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">import</span> std.stdio;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> main()
{
info(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">1, <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;abc&quot;</span>, 2.3</span>);
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The following implementation simply prints information about the arguments by
iterating over them in a<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">foreach</code><span class="Apple-converted-space">&nbsp;</span>loop:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> info(T...)(T args)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// &#39;args&#39; is being used like a tuple:
</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">foreach</span> (i, arg; <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">args</span>) {
writefln(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;%s: %s argument %s&quot;</span>,
i, <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">typeof</span>(arg).stringof, arg);
}
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The output:</p>
<pre class="shell" style="margin: 0.5em 1em 1em; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(224, 224, 224); font-weight: bold; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">0: int argument 1
1: string argument abc
2: double argument 2.3
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Note that instead of obtaining the type of each argument by<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">typeof(arg)</code>,
we could have used<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">T[i]</code><span class="Apple-converted-space">&nbsp;</span>as
well.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
We know that template arguments can be deduced for function templates. That&#39;s
why the compiler deduces the types as<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">int</code>,<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">string</code>,
and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">double</code>in
the previous program.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
However, it is also possible to specify template parameters explicitly. For
example,<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">std.conv.to</code><span class="Apple-converted-space">&nbsp;</span>takes
the destination type as an explicit template parameter:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"> to!<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">string</span>(42);
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
When template parameters are explicitly specified, they can be a mixture of
value, type, and other kinds. That flexibility makes it necessary to be able to
determine whether each template parameter is a type or not, so that the body of
the template can be coded accordingly. That is achieved by treating the
arguments as a<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">TypeTuple</code>.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Let&#39;s see an example of this in a function template that produces<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">struct</code><span class="Apple-converted-space">&nbsp;</span>definitions
as source code in text form. Let&#39;s have this function return the produced source
code as<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">string</code>.
This function can first take the name of the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">struct</code><span class="Apple-converted-space">&nbsp;</span>followed
by the types and names of the members specified as pairs:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">import</span> std.stdio;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> main()
{
writeln(structDefinition!(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;Student&quot;</span>,
string, <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;name&quot;</span>,
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span>, <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;id&quot;</span>,
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span>[], <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;grades&quot;</span>)());
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
That<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">structDefinition</code><span class="Apple-converted-space">&nbsp;</span>instantiation
is expected to produce the following<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">string</code>:</p>
<pre class="shell" style="margin: 0.5em 1em 1em; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(224, 224, 224); font-weight: bold; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">struct Student
{
string name;
int id;
int[] grades;
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
<b style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Note:</b><span class="Apple-converted-space">&nbsp;</span>Functions that produce
source code are useful with the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">mixin</code>keyword,
which we will see in a later chapter.</i></p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The following is an implementation that produces the desired output. Note how
the function template makes use of the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">is</code><span class="Apple-converted-space">&nbsp;</span>expression.
Remember that the expression<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">is&nbsp;(arg)</code><span class="Apple-converted-space">&nbsp;</span>produces<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">true</code><span class="Apple-converted-space">&nbsp;</span>when<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">arg</code><span class="Apple-converted-space">&nbsp;</span>is
a valid type:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">import</span> std.string;
string structDefinition(string name, <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">Members</span>...)()
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">/* Ensure that members are specified as pairs: first the
* type then the name. */</span>
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">static</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">assert</span>((<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">Members</span>.length % 2) == 0,
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;Members must be specified as pairs.&quot;</span>);
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">/* The first part of the struct definition. */</span>
string result = <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;struct &quot;</span> ~ name ~ <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;\n{\n&quot;</span>;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">foreach</span> (i, arg; <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">Members</span>) {
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">static</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">if</span> (i % 2) {
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">/* The odd numbered arguments should be the names
* of members. Instead of dealing with the names
* here, we use them as Members[i+1] in the &#39;else&#39;
* clause below.
*
* Let&#39;s at least ensure that the member name is
* specified as a string. */</span>
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">static</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">assert</span> (<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">is</span> (<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">typeof</span>(arg) == string),
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;Member name &quot;</span> ~ arg.stringof ~
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot; is not a string.&quot;</span>);
} <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">else</span> {
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">/* In this case &#39;arg&#39; is the type of the
* member. Ensure that it is indeed a type. */</span>
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">static</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">assert</span> (<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">is</span> (arg),
arg.stringof ~ <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot; is not a type.&quot;</span>);
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">/* Produce the member definition from its type and
* its name.
*
* Note: We could have written &#39;arg&#39; below instead
* of Members[i]. */</span>
result ~= format(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot; %s %s;\n&quot;</span>,
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">Members[i]</span>.stringof, <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">Members[i+1]</span>);
}
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">/* The closing bracket of the struct definition. */</span>
result ~= <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;}&quot;</span>;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">return</span> result;
}
</pre>
<h5 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.25em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Template specializations</h5>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
We have seen template specializations in<span class="Apple-converted-space">&nbsp;</span><a href="http://ddili.org/ders/d.en/templates.html" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: purple; text-decoration: none; background-position: initial initial; background-repeat: initial initial;">the
Templates chapter</a>. Like type parameters, other kinds of template parameters
can be specialized as well. The following is both the general definition of a
template and its specialization for 0:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> foo(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> value)()
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ... general definition ...
</span>}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> foo(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> value <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">: 0</span>)()
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ... special definition for zero ...
</span>}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
We will take advantage of template specializations in the<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">meta
programming</i><span class="Apple-converted-space">&nbsp;</span>section below.</p>
<h5 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.25em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Meta programming</h5>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
As they are about code generation, templates are among the higher level features
of D. A template is indeed code that generates code. Writing code that generates
code is called<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">meta
programming</i>.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Due to templates being compile-time features, some operations that are normally
executed at runtime can be moved to compile time as template instantiations.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
(<i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;"><b style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">Note:</b><span class="Apple-converted-space">&nbsp;</span>Compile
time function execution</i><span class="Apple-converted-space">&nbsp;</span>(CTFE)<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">is
another feature that achieves the same goal. We will see CTFE in a later
chapter.</i>)</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Executing</i><span class="Apple-converted-space">&nbsp;</span>templates at run time
is commonly based on recursive template instantiations.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
To see an example of this, let&#39;s first consider a regular function that
calculates the sum of numbers from 0 to a specific value. For example, when its
argument is 4, this fuction should return the result of 0+1+2+3+4:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> sum(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> last)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> result;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">foreach</span> (value; 0 .. last + 1) {
result += value;
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">return</span> result;
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
That is an iterative implementation of the function. The same function can be
implemented by recursion as well:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> sum(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> last)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">return</span> (last == 0
? last
: last + <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">sum</span>(last - 1));
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The recursive function returns the sum of the last value and the previous sum.
As you can see, the function terminates the recursion by treating the value 0
specially.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Functions are normally run-time features. As usual,<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">sum()</code><span class="Apple-converted-space">&nbsp;</span>can
be executed at run time:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"> writeln(sum(4));
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
When the result is needed at compile time, one way of achieving the same
calculation is by defining a function template. In this case, the parameter must
be a template parameter, not a function parameter:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// WARNING: This code is incorrect.
</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> sum(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> last</span>)()
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">return</span> (last == 0
? last
: last + sum<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">!(last - 1)</span>());
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
That function template instantiates itself by<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">last
- 1</code><span class="Apple-converted-space">&nbsp;</span>and tries to calculate the
sum again by recursion. However, that code is incorrect.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
As the ternary operator would be compiled to be executed at run time, there is
no condition check that terminates the recursion at compile time:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"> writeln(sum!4()); <span class="d_comment" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ← <span class="d_hata" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 64, 64); color: rgb(0, 0, 0); background-position: initial initial; background-repeat: initial initial;">compilation ERROR</span></span>
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The compiler detects that the template instances would recurse infinitely and
stops at an arbitrary number of recursions:</p>
<pre class="shell" style="margin: 0.5em 1em 1em; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(224, 224, 224); font-weight: bold; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">Error: template instance deneme.sum!(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">-296</span>) recursive expansion
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Considering the difference between the template argument 4 and -296, the
compiler restricts template expansion at 300 by default.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
In meta programming, recursion is terminated by a template specialization. The
following specialization for 0 produces the expected result:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// The general definition
</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> sum(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> last)()
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">return</span> last + sum!(last - 1)();
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// The special definition for zero
</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> sum(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> last <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">: 0</span>)()
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">return</span> 0;
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The following is a program that tests<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">sum()</code>:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">import</span> std.stdio;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> main()
{
writeln(sum!4());
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Now the program compiles successfully and produces the result of 4+3+2+1+0:</p>
<pre class="shell" style="margin: 0.5em 1em 1em; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(224, 224, 224); font-weight: bold; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">10
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
An important point to make here is that the function<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">sum!4()</code><span class="Apple-converted-space">&nbsp;</span>is
executed entirely at compile time. The compiled code is the equivalent of
calling<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">writeln()</code><span class="Apple-converted-space">&nbsp;</span>with
literal<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">10</code>:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"> writeln(10); <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// the equivalent of writeln(sum!4())
</span></pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
As a result, the compiled code is as fast and simple as can be. Although the
value 10 is still calculated as the result of 4+3+2+1+0, the entire calculation
happens at compile time.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The previous example demonstrates one of the benefits of meta programming:
moving operations from run time to compile time. CTFE obviates some of the
idioms of meta programming in D.</p>
<h5 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.25em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Compile-time polymorphism</h5>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
In object oriented programming (OOP), polymorphism is achieved by inheritance.
For example, if a function takes an interface, it accepts objects of any class
that inherits that interface.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Let&#39;s recall an earlier example from a previous chapter:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">import</span> std.stdio;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">interface</span> SoundEmitter
{
string emitSound();
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">class</span> Violin : SoundEmitter
{
string emitSound()
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">return</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;♩♪♪&quot;</span>;
}
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">class</span> Bell : SoundEmitter
{
string emitSound()
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">return</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;ding&quot;</span>;
}
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> useSoundEmittingObject(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">SoundEmitter object</span>)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ... some operations ...
</span> writeln(object.emitSound());
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ... more operations ...
</span>}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> main()
{
useSoundEmittingObject(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">new</span> Violin);
useSoundEmittingObject(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">new</span> Bell);
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">
useSoundEmittingObject()</code><span class="Apple-converted-space">&nbsp;</span>is
benefiting from polymorphism. It takes a<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">SoundEmitter</code><span class="Apple-converted-space">&nbsp;</span>so
that it can be used with any type that is derived from that interface.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Since<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">working
with any type</i><span class="Apple-converted-space">&nbsp;</span>is inherent to
templates, they can be seen as providing a kind of polymorphism as well. Being a
compile-time feature, the polymorphism that templates provide is called<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">compile-time
polymorphism</i>. Conversely, OOP&#39;s polymorphism is called<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">run-time
polymorphism</i>.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
In reality, neither kind of polymorphism allows being able to be used with<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">any
type</i><span class="Apple-converted-space">&nbsp;</span>because the types must
satisfy certain requirements.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Run-time polymorphism requires that the type implements a certain interface.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Compile-time polymorphism requires that the type is compatible with how it is
used by the template. As long as the code compiles, the template argument can be
used with that template. (<i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;"><b style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">Note:</b><span class="Apple-converted-space">&nbsp;</span>The
argument must satisfy template constraints as well. We will see template
constraints later below.</i>)</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
For example, if<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">useSoundEmittingObject()</code><span class="Apple-converted-space">&nbsp;</span>were
implemented as a function template instead of a function, it could be used with
any type that supported the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">object.emitSound()</code><span class="Apple-converted-space">&nbsp;</span>call:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> useSoundEmittingObject<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">(T)</span>(T object)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ... some operations ...
</span> writeln(object.emitSound());
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ... more operations ...
</span>}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">class</span> Car
{
string emitSound()
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">return</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;honk honk&quot;</span>;
}
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>
useSoundEmittingObject(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">new</span> Violin);
useSoundEmittingObject(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">new</span> Bell);
useSoundEmittingObject(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">new</span> Car);
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Note that although<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">Car</code><span class="Apple-converted-space">&nbsp;</span>has
no inheritance relationship with any other type, the code compiles successfully,
and the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">emitSound()</code><span class="Apple-converted-space">&nbsp;</span>member
function of each type gets called.</p>
<h5 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.25em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Code bloat</h5>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The code generated by the compiler is different for every different argument of
a type parameter, of a value parameter, etc.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The reason for that can be seen by considering<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">int</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">double</code><span class="Apple-converted-space">&nbsp;</span>as
type template arguments. Each type would have to be processed by different kinds
of CPU registers. For that reason, the same template needs to be compiled
differently for different template arguments. In other words, the compiler needs
to generate different code for each instantiation of a template.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
For example, if<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">useSoundEmittingObject()</code><span class="Apple-converted-space">&nbsp;</span>were
implemented as a template, it would be compiled as many times as the number of
different instantiations of it.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Because it results in larger program size, this effect is called<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">code
bloat</i>. Although this is not a problem in most programs, it is an effect of
templates that must be known.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Conversely, non-templated version of<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">useSoundEmittingObject()</code><span class="Apple-converted-space">&nbsp;</span>would
not have any code repetition. The compiler would compile that function just once
and execute the same code for all types of the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">SoundEmitter</code>interface.
In run-time polymorphism, having the same code behave differently for different
types is achieved by function pointers on the background. Although function
pointers have a small cost at run time, that cost is not significant in most
programs.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Since both code bloat and run-time polymorphism have effects on program
performance, it cannot be known beforehand whether run-time polymorphism or
compile-time polymorphism would be a better approach for a specific program.</p>
<h5 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.25em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Template constraints</h5>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The fact that templates can be instantiated with any argument yet not every
argument is compatible with every template brings an inconvenience. If a
template argument is not compatible with a particular template, the
incompatibility is necessarily detected during the compilation of the template
code for that argument. As a result, the compilation error points at a line
inside the template implementation.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Let&#39;s see this by using<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">useSoundEmittingObject()</code><span class="Apple-converted-space">&nbsp;</span>with
a type that does not support the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">object.emitSound()</code><span class="Apple-converted-space">&nbsp;</span>call:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">class</span> Cup
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ... does not have emitSound() ...
</span>}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>
useSoundEmittingObject(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">new</span> Cup); <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ← incompatible type
</span></pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Although arguably the error is with the code that uses the template with an
incompatible type, the compilation error points at a line inside the template:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> useSoundEmittingObject(T)(T object)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ... some operations ...
</span> writeln(object.emitSound()); <span class="d_comment" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ← <span class="d_hata" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 64, 64); color: rgb(0, 0, 0); background-position: initial initial; background-repeat: initial initial;">compilation ERROR</span></span>
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ... more operations ...
</span>}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
An undesired consequence is that when the template is a part of a third-party
library module, the compilation error would appear to be a problem with the
library itself.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Note that this issue does not exist for interfaces: A function that takes an
interface can only be called with a type that implements that interface.
Attempting to call such a function with any other type is a compilation error at
the caller.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Template contraints are for disallowing incorrect instantiations of templates.
They are defined as logical expressions of an<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">if</code><span class="Apple-converted-space">&nbsp;</span>condition
right before the template body:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> foo(T)()
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">if</span> (<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">/* ... <i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">constraints</i> ... */</span>)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
A template definition is considered by the compiler only if its constraints
evaluate to<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">true</code><span class="Apple-converted-space">&nbsp;</span>for
a specific instantiation of the template. Otherwise, the template definition is
ignored for that use.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Let&#39;s see an example of this with a template that calculates the factorial of a
number at compile time:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">import</span> std.stdio;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">ulong</span> factorial(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">ulong</span> number)()
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">return</span> number * factorial!(number - 1)();
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">ulong</span> factorial(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">ulong</span> number : 0)()
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">return</span> 1;
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> main()
{
writefln(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;19! == %26s&quot;</span>, factorial!19());
writefln(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;20! == %26s&quot;</span>, factorial!20());
writefln(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;21! == %26s&quot;</span>, factorial!21());
writeln();
writefln(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;(ulong.max : %s)&quot;</span>, <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">ulong</span>.max);
writefln(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;(actual 21!: 51090942171709440000)&quot;</span>);
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The program above calculates the factorials of 19, 20, and 21. (<i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;"><b style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">Note:</b>The
same calculations could be performed at compile time by CTFE as well.</i>)</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
However, since the factorial of 21 cannot be represented by<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">ulong</code><span class="Apple-converted-space">&nbsp;</span>the
program produces an incorrect result:</p>
<pre class="shell" style="margin: 0.5em 1em 1em; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(224, 224, 224); font-weight: bold; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">19! == 121645100408832000
20! == 2432902008176640000
21! == 14197454024290336768 <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">← incorrect result</span>
(ulong.max : 18446744073709551615)
(actual 21!: 51090942171709440000)
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
A template constraint can prevent using<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">factorial</code><span class="Apple-converted-space">&nbsp;</span>with
incorrect values:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">ulong</span> factorial(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">ulong</span> number)()
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">if</span> (number &lt;= 20)</span>
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">return</span> number * factorial!(number - 1)();
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Now the compiler will consider the template only for values that satisfy that
condition and reject the others:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"> writefln(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: red; background-position: initial initial; background-repeat: initial initial;">&quot;21! == %26s&quot;</span>, factorial!21()); <span class="d_comment" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ← <span class="d_hata" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 64, 64); color: rgb(0, 0, 0); background-position: initial initial; background-repeat: initial initial;">compilation ERROR</span></span>
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The compilation error indicates that there is no template that matches the call<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">factorial!21()</code>:</p>
<pre class="shell" style="margin: 0.5em 1em 1em; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(224, 224, 224); font-weight: bold; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">Error: template deneme.factorial <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">does not match</span> any function
template declaration. Candidates are:
...
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Since templates are a compile-time feature, template constraints must be
evaluable at compile time. The<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">is</code><span class="Apple-converted-space">&nbsp;</span>expression
that we have seen in<a href="http://ddili.org/ders/d.en/is_expr.html" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: purple; text-decoration: none; background-position: initial initial; background-repeat: initial initial;">the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: purple; background-position: initial initial; background-repeat: initial initial;">is</code><span class="Apple-converted-space">&nbsp;</span>Expression
chapter</a><span class="Apple-converted-space">&nbsp;</span>is commonly used in
template constraints. We will use the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">is</code><span class="Apple-converted-space">&nbsp;</span>expression
in the following examples as well.</p>
<h6 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.1em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Tuple parameter of single element</h6>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Sometimes the single parameter of a template needs to be one of type, value, or<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">alias</code><span class="Apple-converted-space">&nbsp;</span>kinds.
That can be achieved by a tuple parameter of length one:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">template</span> myTemplate(T...)
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">if</span> (T.length == 1)</span>
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">static</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">if</span> (<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">is</span> (<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">T[0]</span>)) {
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// The single parameter is a type
</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">enum</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">bool</span> myTemplate = <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">/* ... */</span>;
} <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">else</span> {
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// The single parameter is some other kind
</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">enum</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">bool</span> myTemplate = <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">/* ... */</span>;
}
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Some of the templates of the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">std.traits</code><span class="Apple-converted-space">&nbsp;</span>module
take advantage of this idiom. We will see<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">std.traits</code><span class="Apple-converted-space">&nbsp;</span>in
a later chapter.</p>
<h6 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.1em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Named constraints</h6>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Sometimes the constraints are complex, making it hard to understand the
requirements of template parameters. This complexity can be handled by an idiom
that effectively gives names to constraints. This idiom combines four features
of D: anonymous functions,<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">typeof</code>,
the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">is</code>expression,
and eponymous templates.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Let&#39;s see this on a function template that has a type parameter. The template
uses its function parameter in specific ways:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> use(T)(T object)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span> object.prepare();
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span> object.fly(42);
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span> object.land();
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
As is obvious from the implementation of the template, the types that this
function can work with must support three specific function calls on the object:<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">prepare()</code>,<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">fly(42)</code>,
and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">land()</code>.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
One way of specifying a template constraint for that type is by the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">is</code>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">typeof</code><span class="Apple-converted-space">&nbsp;</span>expressions
for each function call inside the template:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> use(T)(T object)
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">if</span> (<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">is</span> (<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">typeof</span>(object.prepare())) &amp;&amp;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">is</span> (<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">typeof</span>(object.fly(1))) &amp;&amp;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">is</span> (<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">typeof</span>(object.land())))
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
I will explain that syntax below. For now, accept the whole construct of<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">is&nbsp;(typeof(object.prepare()))</code><span class="Apple-converted-space">&nbsp;</span>to
mean<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">whether
the type supports the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">.prepare()</code><span class="Apple-converted-space">&nbsp;</span>call</i>.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Although such constraints achieve the desired goal, sometimes they are too
complex to be readable. Instead, it is possible to give a more descriptive name
to the whole constraint:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> use(T)(T object)
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">if</span> (canFlyAndLand!T)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
That constraint is more readable because it is now more clear that the template
is designed to work with types that<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">can
fly and land</i>.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Such constraints are achieved by an idiom that is implemented similar to the
following eponymous template:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">template</span> canFlyAndLand(T)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">enum</span> canFlyAndLand = <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">is</span> (<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">typeof</span>(
{
T object;
object.prepare(); <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// should be preparable for flight
</span> object.fly(1); <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// should be flyable for a certain distance
</span> object.land(); <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// should be landable
</span> }()));
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The D features that take part in that idiom and how they interact with each
other are explained below:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">template</span> canFlyAndLand(T)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// (6) (5) (4)
</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">enum</span> canFlyAndLand = <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">is</span> (<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">typeof</span>(
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">{</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// (1)
</span> T object; <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// (2)
</span> object.prepare();
object.fly(1);
object.land();
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// (3)
</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">}</span>()));
}
</pre>
<ol style="margin: 1em 1em 0.75em 1.5em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
<b style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Anonymous function:</b><span class="Apple-converted-space">&nbsp;</span>We have
seen anonymous functions in<span class="Apple-converted-space">&nbsp;</span><a href="http://ddili.org/ders/d.en/lambda.html" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: purple; text-decoration: none; background-position: initial initial; background-repeat: initial initial;">the
Function Pointers, Delegates, and Lambdas chapter</a>. The highlighted curly
brackets above define an anonymous function.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
<b style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Function block:</b><span class="Apple-converted-space">&nbsp;</span>The function
block uses the type as it is supposed to be used in the actual template.
First an object of that type is defined and then that object is used in
specific ways. (This code never gets executed; see below.)</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
<b style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Evaluation of the function:</b><span class="Apple-converted-space">&nbsp;</span>The
empty parentheses at the end of an anonymous function normally execute that
function. However, since that call syntax is within a<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">typeof</code>,
it is never executed.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
<b style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
The<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">typeof</code><span class="Apple-converted-space">&nbsp;</span>expression:</b><span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">typeof</code><span class="Apple-converted-space">&nbsp;</span>produces
the type of an expression.<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); background-position: initial initial; background-repeat: initial initial;">
An important fact about<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">typeof</code><span class="Apple-converted-space">&nbsp;</span>is
that it never executes the expression. Rather, it produces the type of the
expression<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">if</i><span class="Apple-converted-space">&nbsp;</span>that
expression would be executed:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial;"> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> i = 42;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">typeof</span>(++i) j; <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// same as &#39;int j;&#39;
</span>
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">assert</span>(i == 42); <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ++i has not been executed
</span></pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); background-position: initial initial; background-repeat: initial initial;">
As the previous<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">assert</code><span class="Apple-converted-space">&nbsp;</span>proves,
the expression<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">++i</code><span class="Apple-converted-space">&nbsp;</span>has
not been executed.<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">typeof</code><span class="Apple-converted-space">&nbsp;</span>has
merely produced the type of that expression as<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">int</code>.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); background-position: initial initial; background-repeat: initial initial;">
If the expression that<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">typeof</code><span class="Apple-converted-space">&nbsp;</span>receives
is not valid,<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">typeof</code>produces
no type at all (not even<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">void</code>).
So, if the anonymous function inside<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">canFlyAndLand</code><span class="Apple-converted-space">&nbsp;</span>can
be compiled successfully for<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">T</code>,<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">typeof</code><span class="Apple-converted-space">&nbsp;</span>produces
a valid type. Otherwise, it produces no type at all.</p>
</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
<b style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
The<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">is</code><span class="Apple-converted-space">&nbsp;</span>expression:</b><span class="Apple-converted-space">&nbsp;</span>We
have seen many different uses of the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">is</code>expression
in<span class="Apple-converted-space">&nbsp;</span><a href="http://ddili.org/ders/d.en/is_expr.html" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: purple; text-decoration: none; background-position: initial initial; background-repeat: initial initial;">the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: purple; background-position: initial initial; background-repeat: initial initial;">is</code><span class="Apple-converted-space">&nbsp;</span>Expression
chapter</a>. The<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">is&nbsp;(<i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">Type</i>)</code><span class="Apple-converted-space">&nbsp;</span>syntax
produces<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">true</code><span class="Apple-converted-space">&nbsp;</span>if<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">Type</code><span class="Apple-converted-space">&nbsp;</span>is
valid:<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial;"> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> i;
writeln(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">is</span> (<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">typeof</span>(i))); <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// true
</span> writeln(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">is</span> (<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">typeof</span>(nonexistentSymbol))); <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// false
</span></pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); background-position: initial initial; background-repeat: initial initial;">
Although the second<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">typeof</code><span class="Apple-converted-space">&nbsp;</span>above
receives a nonexistent symbol, the compiler does not emit a compilation
error. Rather, the effect is that the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">typeof</code><span class="Apple-converted-space">&nbsp;</span>expression
does not produce any type, so the<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">is</code><span class="Apple-converted-space">&nbsp;</span>expression
produces<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">false</code>:</p>
<pre class="shell" style="margin: 0.5em 1em 1em; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(224, 224, 224); font-weight: bold; background-position: initial initial; background-repeat: initial initial;">true
false
</pre>
</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
<b style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Eponymous template:</b><span class="Apple-converted-space">&nbsp;</span>As
described above, since the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">canFlyAndLand</code>template
contains a definition by the same name, the template instantiation is that
definition itself.</li>
</ol>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
In the end,<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">use()</code><span class="Apple-converted-space">&nbsp;</span>gains
a more descriptive constraint:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> use(T)(T object)
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">if</span> (canFlyAndLand!T)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Let&#39;s try to use that template with two types, one that satisfies the constraint
and one that does not satisfy the constraint:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// A type that does match the template&#39;s operations
</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">class</span> ModelAirplane
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> prepare()
{}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> fly(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> distance)
{}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> land()
{}
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// A type that does not match the template&#39;s operations
</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">class</span> Pigeon
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> fly(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">int</span> distance)
{}
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>
use(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">new</span> ModelAirplane); <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ← compiles
</span> use(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">new</span> Pigeon); <span class="d_comment" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ← <span class="d_hata" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 64, 64); color: rgb(0, 0, 0); background-position: initial initial; background-repeat: initial initial;">compilation ERROR</span></span>
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Named or not, since the template has a constraint, the compilation error points
at the line where the template is used rather than where it is implemented.</p>
<h5 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.25em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Summary</h5>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The earlier template chapter had the following reminders:</p>
<ul style="margin: 0.5em 1em 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Templates define the code as a pattern, for the compiler to generate
instances of it according to the actual uses in the program.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Templates are a compile-time feature.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Specifying template parameter lists is sufficient to make function, struct,
and class definitions templates.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Template arguments can be specified explicitly after an exclamation mark.
The parenteses are not necessary when there is only one token inside the
parenteses.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Each different instantiation of a template is a different type.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Template arguments can only be deduced for function templates.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Templates can be specialized for the type that is after the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">:</code>character.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Default template arguments are specified after the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">=</code><span class="Apple-converted-space">&nbsp;</span>character.</li>
</ul>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
This chapter added the following concepts:</p>
<ul style="margin: 0.5em 1em 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Templates can be defined by the full syntax or the shortcut syntax.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
The scope of the template is a name space.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
A template that contains a definition with the same name as the template is
called an eponymous template. The template represents that definition.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Templates can be of functions, classes, structs, unions, and interfaces, and
every template body can contain any number of definitions.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Template parameters can be of type, value,<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">this</code>,<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">alias</code>,
and tuple kinds.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Templates can be specialized for particular arguments.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Meta programming is a way of executing operations at compile time.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Templates enable<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">compile-time
polymorphism</i>.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Separate code generation for different instantiations can cause<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">code
bloat</i>.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Template constraints limit the uses of templates for specific template
arguments. They help move compilation errors from the implementations of
templates to where the templates are actually used incorrectly.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
It is more readable to give names to template constraints.</li>
</ul>
<div id="ders_nav_son" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 0.9em; vertical-align: baseline; background-color: transparent; float: right; width: 190px; text-align: center; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
[<span class="Apple-converted-space">&nbsp;</span><a href="tuples.html" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: purple; text-decoration: none; background-position: initial initial; background-repeat: initial initial;">Prev&nbsp;</a>]
&nbsp; [<span class="Apple-converted-space">&nbsp;</span><a href="functionsmore.html" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: purple; text-decoration: none; background-position: initial initial; background-repeat: initial initial;">Next&nbsp;</a>]</div>
</body>
</html>
D
1
https://gitee.com/lucifer2031/Programming-in-D-in-Chinese.git
git@gitee.com:lucifer2031/Programming-in-D-in-Chinese.git
lucifer2031
Programming-in-D-in-Chinese
Programming-in-D-in-Chinese
master

搜索帮助