8 Star 183 Fork 8

Lucifer / Programming-in-D-in-Chinese

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
templates.html 264.64 KB
一键复制 编辑 原始数据 按行查看 历史
Lucifer 提交于 2014-02-06 17:05 . Programming in D in English
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
<!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>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;">
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;">
Templates are the feature that allows describing the code as a pattern, for the
compiler to generate program code automatically. Parts of the source code may be
left to the compiler to be filled in until that part is actually used in the
program. The compiler fills in the missing parts.</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 are very useful especially in libraries because they enable writing
generic algorithms and data structures, instead of tying them to 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;">
Compared to the template supports in other languages, D&#39;s templates are very
powerful and extensive. I will not get into all of the details of templates in
this chapter. I will cover only function, struct, and class templates and only<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. For a complete reference on D templates, see<span class="Apple-converted-space">&nbsp;</span><a href="https://github.com/PhilippeSigaud/D-templates-tutorial" 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;">Philippe
Sigaud&#39;s<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;">D
Templates: A Tutorial</i></a>.</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 benefits of templates let&#39;s start with a function that prints values
in parentheses:</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> printInParens(<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)
{
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)&quot;</span>, 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;">
Because the parameter is specified 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>,
that function can only be used with values 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;">int</code>,
or values that can automatically be converted 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;">int</code>.
For example, the compiler would not allow calling it with a floating point 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;">
Let&#39;s assume that the requirements of a program changes and that other types
need to be printed in parentheses as well. One of the solutions for this would
be to take advantage of function overloading and provide overloads of the
function for all of the types that the function is used 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: green; background-position: initial initial; background-repeat: initial initial;">// The function that already exists
</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> printInParens(<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)
{
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)&quot;</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;">// Overloading the function for &#39;double&#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;">void</span> printInParens(<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> value)
{
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)&quot;</span>, 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;">
This solution does not scale well because this time the function cannot be used
with e.g.<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;">real</code><span class="Apple-converted-space">&nbsp;</span>or
any user-defined type. Although it is possible to overload the function for
other types, the cost of doing this may be prohibitive.</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;">
An important observation here is that regardless of the type of the parameter,
the contents of the overloads would all 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;">generically</i><span class="Apple-converted-space">&nbsp;</span>the
same: a single<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;">writefln()</code><span class="Apple-converted-space">&nbsp;</span>expression.</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 duplication is common in algorithms and data structures. For example, the
binary search algorithm is independent of the type of the elements: It is about
the specific steps and operations of the search. Similarly, the linked list data
structure is independent of the type of the elements: Linked list is merely
about<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;">how</i><span class="Apple-converted-space">&nbsp;</span>the
elements are stored in the container, regardless of their 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 are useful in such situations: Once a piece of code is described as a
template, the compiler generates overloads of the same code automatically
according to the actual uses of that code in the 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;">
As I have mentioned above, in this chapter I will cover only function, struct,
and class templates, and<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.</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;">
Function templates</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;">
Defining a function as a template is leaving one or more of the types that it
uses as unspecified, to be deduced later by the compiler.</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 types that are being left unspecified are defined within the template
parameter list, which comes between the name of the function and the function
parameter list. For that reason, function templates have two parameter lists:
the template parameter list and the function 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;"><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> printInParens<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)
{
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)&quot;</span>, 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<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>within
the template parameter list above means 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;">T</code><span class="Apple-converted-space">&nbsp;</span>can
be any type. 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;">T</code><span class="Apple-converted-space">&nbsp;</span>is
an arbitrary name, it is an acronym for &quot;type&quot; and is very common in 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;">
Since<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>represents
any type, the templated 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;">printInParens()</code><span class="Apple-converted-space">&nbsp;</span>above
is sufficient to use it with almost every type, including the user-defined ones:</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> printInParens(T)(T value)
{
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)&quot;</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;">void</span> main()
{
printInParens(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;">// with int
</span> printInParens(1.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;">// with 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;">auto</span> myValue = MyStruct();
printInParens(myValue); <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;">// with MyStruct
</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> MyStruct
{
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> <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;hello&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 compiler considers all of the uses 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;">printInParens()</code><span class="Apple-converted-space">&nbsp;</span>in
the program and generates code to support all those uses. The program is then
compiled as if the function has been overloaded explicitly 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;">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>,
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;">MyStruct</code>:</p>
<pre class="mono" 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;">/*
* Note: These functions are not part of the source
* code. They are the equivalents of the functions that
* the compiler would automatically generate.
*/
void printInParens(<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;">int</span> value)
{
writefln(&quot;(%s)&quot;, value);
}
void printInParens(<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;">double</span> value)
{
writefln(&quot;(%s)&quot;, value);
}
void printInParens(<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> value)
{
writefln(&quot;(%s)&quot;, 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 output of the program contains different lines from the different<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;">instantiations</i><span class="Apple-converted-space">&nbsp;</span>by
the compiler of the function 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;">(42)
(1.2)
(hello)
</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;">
More than one template parameter</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;">
Let&#39;s change the function template to take also the parenthesis characters:</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> printInParens(T)(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;">char</span> opening, <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;">char</span> closing)
{
writeln(opening, value, closing);
}
</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 we can call the same function with different sets of parentheses:</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;"> printInParens(42, &#39;&lt;&#39;, &#39;&gt;&#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;">
Although being able to specify the parentheses makes the function more usable,
specifying the type of the parentheses 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;">char</code><span class="Apple-converted-space">&nbsp;</span>makes
it less flexible because it is not possible to call the function with characters
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;">wchar</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;">dchar</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;"> printInParens(42, &#39;→&#39;, &#39;←&#39;); <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>
<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.printInParens(T) cannot deduce
template function from argument types !()(int,<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;">wchar</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;">wchar</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;">
One solution would be to specify the type of the parentheses 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;">dchar</code>but
this would still be insufficient as this time the function could not be called
e.g. 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;">string</code><span class="Apple-converted-space">&nbsp;</span>or
user-defined 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;">
Another solution is to leave the type of the parentheses to the compiler as
well. Defining an additional template parameter instead of the specific<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;">char</code><span class="Apple-converted-space">&nbsp;</span>is
sufficient:</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> printInParens(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;">, ParensType</span>)(T 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;">ParensType</span> opening,
<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;">ParensType</span> closing)
{
writeln(opening, value, closing);
}
</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 meaning of the new template parameter is 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;">T</code>&#39;s:<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;">ParensType</code><span class="Apple-converted-space">&nbsp;</span>can
be 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;">
It is now possible to use many different types of parentheses. The following are
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;">wchar</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;">string</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;"> printInParens(42, &#39;→&#39;, &#39;←&#39;);
printInParens(1.2, <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: red; background-position: initial initial; background-repeat: initial initial;">&quot;=-&quot;</span>);
</pre>
<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;">→42←
-=1.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;">
The flexibility 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;">printInParens()</code><span class="Apple-converted-space">&nbsp;</span>has
been increased, as it now works correctly for any combination 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>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;">ParensType</code><span class="Apple-converted-space">&nbsp;</span>as
long as those types are printable 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;">writeln()</code>.</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;">
Type deduction</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 compiler&#39;s deciding on what type to use for a template parameter 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;">type
deduction</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;">
Continuing from the last example above, the compiler decides on the following
types according to the two uses of the function template:</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;">
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;">wchar</code><span class="Apple-converted-space">&nbsp;</span>when
42 is printed</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;">
double</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;">string</code><span class="Apple-converted-space">&nbsp;</span>when
1.2 is printed</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 compiler can deduce types only from the types of the parameter values that
are passed to function templates. Although the compiler can usually deduce the
types without any ambiguity, there are times when the types must be specified
explicitly by the programmer.</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;">
Explicit type specification</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;">
Sometimes it is not possible for the compiler to deduce the template parameters.
A situation that this can happen is when the types do not appear in the function
parameter list. When template parameters are not related to function parameters,
the compiler cannot deduce the template parameter 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;">
To see an example of this, let&#39;s design a function that asks a question to the
user, reads a value as a response, and returns that value. Additionally, let&#39;s
make this a function template so that it can be used to read any type of
response:</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: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">T</span> getResponse<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>(string question)
{
writef(<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): &quot;</span>, question, T.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;">T</span> response;
readf(<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&quot;</span>, &amp;response);
<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> response;
}
</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 would be very useful in programs to read different types
of values from the input. For example, to read some user information, we can
imagine calling it as in the following line:</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;"> getResponse(<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;What is your age?&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;">
Unfortunately, that call does not give the compiler any clue as to what 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>should
be. What is known is that the question is passed to the function 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>,
but the type of the return value cannot be deduced:</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.getResponse(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;">cannot deduce</span> template
function from argument types !()(string)
</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 such cases, the template parameters must be specified explicitly by the
programmer. Template parameters are specified in parentheses after an
exclamation mark:</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;"> getResponse<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>(<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;What is your age?&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 code above can now be accepted by the compiler and the function template is
compiled 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;">T</code><span class="Apple-converted-space">&nbsp;</span>referring
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;">int</code><span class="Apple-converted-space">&nbsp;</span>within
the definition 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;">
When there is only one template parameter specified, the parenteses around it
are optional:</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;"> getResponse<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>(<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;What is your age?&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;">// same as above
</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;">
You may recognize that syntax from<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;">to!string</code>,
which we have been using in earlier programs.<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;">to()</code><span class="Apple-converted-space">&nbsp;</span>is
a function template, which takes the target type of the conversion as a template
parameter. Since it has only one template parameter that needs to be specified,
it is commonly written 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;">to!string</code><span class="Apple-converted-space">&nbsp;</span>instead
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;">to!(string)</code>.</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 instantiation</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;">
Automatic generation of code for different sets of template parameter types is
called an<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;">instantiation</i><span class="Apple-converted-space">&nbsp;</span>of
that template for that specific set of 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;">to!string</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;">to!int</code><span class="Apple-converted-space">&nbsp;</span>are
two distinct instantiations 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;">to</code><span class="Apple-converted-space">&nbsp;</span>function
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 I will mention again in a separate section below, distinct instantiations of
templates produce distinct and incompatible types.</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 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;">
Although 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;">getResponse()</code><span class="Apple-converted-space">&nbsp;</span>function
template can in theory be used for any template type, the code that the compiler
generates may not be suitable for every type. Let&#39;s assume that we have the
following type that represents points on a two dimensional space:</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
{
<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;
<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;
}
</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 instantiation 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;">getResponse()</code><span class="Apple-converted-space">&nbsp;</span>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;">Point</code><span class="Apple-converted-space">&nbsp;</span>type
itself would be fine, the generated<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;">readf()</code><span class="Apple-converted-space">&nbsp;</span>call
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;">Point</code><span class="Apple-converted-space">&nbsp;</span>cannot
be compiled. This is because the standard library 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;">readf()</code><span class="Apple-converted-space">&nbsp;</span>does
not know how to read 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;">Point</code><span class="Apple-converted-space">&nbsp;</span>object.
The two lines that actually read the response would look like the following in
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;">Point</code><span class="Apple-converted-space">&nbsp;</span>instantiation
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;">getResponse()</code><span class="Apple-converted-space">&nbsp;</span>function
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;"> Point response;
readf(<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&quot;</span>, &amp;response); <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;">
One way of reading 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;">Point</code><span class="Apple-converted-space">&nbsp;</span>object
would be to read the values 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;">x</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;">y</code><span class="Apple-converted-space">&nbsp;</span>members
separately and then to<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;">construct</i><span class="Apple-converted-space">&nbsp;</span>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;">Point</code><span class="Apple-converted-space">&nbsp;</span>object
from those values.</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;">
Providing a special definition of a template for a specific type 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;">template
specialization</i>. The specialization is defined by the type name after 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;">:</code><span class="Apple-converted-space">&nbsp;</span>character
in the template parameter list. 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;">Point</code>specialization
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;">getResponse()</code><span class="Apple-converted-space">&nbsp;</span>function
template can be defined 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: green; background-position: initial initial; background-repeat: initial initial;">// The general definition of the function template (same as before)
</span>T getResponse(T)(string question)
{
writef(<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): &quot;</span>, question, T.stringof);
T response;
readf(<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&quot;</span>, &amp;response);
<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> response;
}
<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 specialization of the function template for Point
</span>T getResponse(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;">: Point</span>)(string question)
{
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 (Point)&quot;</span>, question);
<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> x = getResponse!<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; x&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;">auto</span> y = getResponse!<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; y&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> Point(x, 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;">
Note that the specialization takes advantage of the general 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;">getResponse()</code><span class="Apple-converted-space">&nbsp;</span>to
read 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;">int</code><span class="Apple-converted-space">&nbsp;</span>values
to be used as the values 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;">x</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;">y</code><span class="Apple-converted-space">&nbsp;</span>members.</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;">
Instead of instantiating the template itself, now the compiler uses the
specialization above whenever<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;">getResponse()</code><span class="Apple-converted-space">&nbsp;</span>is
called 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;">Point</code>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> center = getResponse!Point(<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;Where is the center?&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;">
Assuming that the user enters 11 and 22:</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;">Where is the center? (Point)
x (int): 11
y (int): 22
</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;">getResponse!int()</code><span class="Apple-converted-space">&nbsp;</span>calls
are directed to the general definition of the template 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;">getResponse!Point()</code><span class="Apple-converted-space">&nbsp;</span>calls
are directed to 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;">Point</code><span class="Apple-converted-space">&nbsp;</span>specialization
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;">
As another example, let&#39;s consider using the same template with<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>.
As you would remember from the<span class="Apple-converted-space">&nbsp;</span><a href="http://ddili.org/ders/d.en/strings.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;">Strings
chapter</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;">readf()</code><span class="Apple-converted-space">&nbsp;</span>would
read all of the characters from the input as part of a single<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>until
the end of the input. For that reason, the default 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;">getResponse()</code><span class="Apple-converted-space">&nbsp;</span>would
not be useful when reading<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>responses:</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;">// Reads the entire input, not only 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;">auto</span> name = getResponse!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;What is your name?&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;">
We can provide a template specialization 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;">string</code><span class="Apple-converted-space">&nbsp;</span>as
well. The following specialization reads just 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;">line</i><span class="Apple-converted-space">&nbsp;</span>instead:</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 getResponse(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;">: string</span>)(string question)
{
writef(<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 (string): &quot;</span>, question);
<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;">// Read and ignore whitespace characters which have
</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;">// presumably been left over from the previous user input
</span> string response;
<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;">do</span> {
response = chomp(readln());
} <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;">while</span> (response.length == 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> response;
}
</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;">
Struct and class templates</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<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;">Point</code><span class="Apple-converted-space">&nbsp;</span>struct
may be seen as having a limitation: Because its two members are defined
specifically 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>,
it cannot represent fractional coordinate values. This limitation can be removed
if 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;">Point</code><span class="Apple-converted-space">&nbsp;</span>struct
is defined as 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;">
Let&#39;s first add a member function that returns the distance to another<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;">Point</code><span class="Apple-converted-space">&nbsp;</span>object:</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.math;
<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> 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> x;
<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;
<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> distanceTo(<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;">in</span> Point that) <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;">immutable</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;">real</span> xDistance = x - that.x;
<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> <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;">real</span> yDistance = y - that.y;
<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> distance = sqrt((xDistance * xDistance) +
(yDistance * yDistance));
<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: blue; background-position: initial initial; background-repeat: initial initial;">cast</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>)distance;
}
}
</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 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;">Point</code><span class="Apple-converted-space">&nbsp;</span>is
suitable when the required precision is relatively low: It can calculate the
distance between two points at kilometer precision, e.g. between the center and
branch offices of an organization:</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> center = getResponse!Point(<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;Where is the center?&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;">auto</span> branch = getResponse!Point(<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;Where is the branch?&quot;</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;Distance: &quot;</span>, center.distanceTo(branch));
</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;">
Unfortunately,<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;">Point</code><span class="Apple-converted-space">&nbsp;</span>is
inadequate at higher precisions than<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>can
provide.</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;">
structs and classes can be defined as templates as well, by specifying a
template parameter list after their names. 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;">Point</code><span class="Apple-converted-space">&nbsp;</span>can
be defined as a struct template by providing a template parameter and replacing
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;">int</code>s
by that 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> Point<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: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;">T</span> 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;">T</span> y;
<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> distanceTo(<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;">in</span> Point that) <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;">immutable</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;">real</span> xDistance = x - that.x;
<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> <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;">real</span> yDistance = y - that.y;
<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> distance = sqrt((xDistance * xDistance) +
(yDistance * yDistance));
<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: blue; background-position: initial initial; background-repeat: initial initial;">cast</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</span>)distance;
}
}
</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 structs and classes are not functions, they cannot be called with
parameters. This makes it impossible for the compiler to deduce their template
parameters. The template parameter list must always be specified for struct and
class templates:</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> center = Point<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>(0, 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;">auto</span> branch = Point<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>(100, 100);
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;Distance: &quot;</span>, center.distanceTo(branch));
</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 definitions above make the compiler generate 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;">int</code>instantiation
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;">Point</code><span class="Apple-converted-space">&nbsp;</span>template,
which is the equivalent of its earlier non-template definition. However, now it
can be used with any type. For example, when more precision is needed, 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;">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> point1 = Point<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>(1.2, 3.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;">auto</span> point2 = Point<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>(5.6, 7.8);
writeln(point1.distanceTo(point2));
</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 template itself has been defined independently of any specific
type, its single definition makes it possible to represent points of various
precisions.</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;">
Simply converting<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;">Point</code><span class="Apple-converted-space">&nbsp;</span>to
a template would cause compilation errors in code that has already been written
according to its non-template definition. For example, now 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;">Point</code><span class="Apple-converted-space">&nbsp;</span>specialization
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;">getResponse()</code><span class="Apple-converted-space">&nbsp;</span>cannot
be compiled:</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 getResponse(T : Point)(string question) <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>
{
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 (Point)&quot;</span>, question);
<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> x = getResponse!<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; x&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;">auto</span> y = getResponse!<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; y&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> Point(x, 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;">
The reason for the compilation error is 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;">Point</code><span class="Apple-converted-space">&nbsp;</span>itself
is not a type anymore:<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;">Point</code><span class="Apple-converted-space">&nbsp;</span>is
now 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;">struct
template</i>. Only instantiations of that template would be considered as types.
The following changes are required to correctly specialize<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;">getResponse()</code><span class="Apple-converted-space">&nbsp;</span>for
any instantiation 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;">Point</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;">Point!T getResponse(T : Point!T)(string question) <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, 1
</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 (Point!%s)&quot;</span>, question, T.stringof); <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;">// 5
</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> x = getResponse!T(<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>); <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;">// 3a
</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> y = getResponse!T(<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>); <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;">// 3b
</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> Point!T(x, y); <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;">// 4
</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;">
In order for this template specialization to support all instantiations 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;">Point</code>,
the template parameter list must mention<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;">Point!T</code>.
This simply means 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;">getResponse()</code><span class="Apple-converted-space">&nbsp;</span>specialization
is 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;">Point!T</code>,
regardless 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>.
This specialization would match<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;">Point!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;">Point!double</code>,
etc.</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;">
Similarly, to return the correct type as the response, the return type must
be specified 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;">Point!T</code><span class="Apple-converted-space">&nbsp;</span>as
well.</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;">
Since the types 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;">x</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;">y</code><span class="Apple-converted-space">&nbsp;</span>members
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;">Point!T</code><span class="Apple-converted-space">&nbsp;</span>are
now<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>,
as opposed 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;">int</code>,
the members must be read by calling<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;">getResponse!T()</code>,
not<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;">getResponse!int()</code>,
as the latter would be correct only 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;">Point!int</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;">
Similar to items 1 and 2, the type of the return value is<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;">Point!T</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;">
To print the name of the type accurately for every type, as in<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;">Point!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;">Point!double</code>,
etc.,<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.stringof</code><span class="Apple-converted-space">&nbsp;</span>is
used.</li>
</ol>
<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;">
Default 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;">
Sometimes it is cumbersome to provide template parameter types every time a
template is used, especially when that type is almost always a particular type.
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;">getResponse()</code><span class="Apple-converted-space">&nbsp;</span>may
almost always be called 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;">int</code><span class="Apple-converted-space">&nbsp;</span>type
in the program, and only in a few places 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;">double</code><span class="Apple-converted-space">&nbsp;</span>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;">
It is possible to specify default types for template parameters, which are
assumed when the types are not explicitly provided. Default parameter types 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:</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 getResponse(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;">int</span></span>)(string question)
{
<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: 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> age = getResponse(<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;What is your age?&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;">
As no type has been specified when 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;">getResponse()</code><span class="Apple-converted-space">&nbsp;</span>above,<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>becomes
the default 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;">int</code><span class="Apple-converted-space">&nbsp;</span>and
the call ends up being the equivalent 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;">getResponse!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); 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;">
Default template parameters can be specified for struct and class templates as
well, but in their case the template parameter list must always be written even
when empty:</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 = <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: 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: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>
Point!<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> center;
</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;">
Similar to default function parameter values as we have seen in the<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;">Variable
Number of Parameters chapter</a>, default template parameters can be specified
for all of the template parameters or for 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;">last</i><span class="Apple-converted-space">&nbsp;</span>ones:</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> myTemplate(T0, T1 <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>, T2 <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;">char</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;">// ...
</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 last two template parameters of that function may be left unspecified but
the first one is required:</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;"> myTemplate!string();
</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 usage, the second and third parameters 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;">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;">char</code>,
respectively.</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;">
Every different template instantiation is a distinct type</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;">
Every instantiation of a template for a given set of types is considered to be a
distinct type. 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;">Point!int</code><span class="Apple-converted-space">&nbsp;</span>and<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;">Point!double</code><span class="Apple-converted-space">&nbsp;</span>are
separate 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;">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> point3 = 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>(0.25, 0.75); <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;">
Those different types cannot be used in the assignment operation above:</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: cannot implicitly convert expression (Point(0.25,0.75))
of type <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;">Point!(double)</span> 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;">Point!(int)</span>
</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;">
A compile-time feature</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;">
Templates are entirely a compile-time feature. The instances of templates are
generated by the compiler at compile time. They cannot be generated at run time.</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;">
Class template example: stack data structure</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;">
Struct and class templates are commonly used in the implementations of data
structures. Let&#39;s design a stack container that will be able to contain 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;">
Stack is one of the simplest data structures. It represents a container where
elements are placed conceptually on top of each other as would be in a stack of
papers. New elements go on top, and only the topmost element is accessed. When
an element is removed, it is always the topmost one.</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 we also define a property that returns the total number of elements in the
stack, all of the operations of this data structure would be the following:</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;">
Add element (<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;">push()</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;">
Remove element (<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;">pop()</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;">
Access the topmost element (<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;">.top</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;">
Report the number of elements (<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;">.length</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;">
An array can be used to store the elements such that the last element of the
array would be representing the topmost element of the stack. Finally, it can be
defined as a class template to be able to contain elements of any 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;">class</span> Stack<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;">private</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</span>[] 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;">public</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> push(<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> element)
{
elements ~= element;
}
<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> pop()
{
--elements.length;
}
<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> top() <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> @property
{
<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> elements[$ - 1];
}
size_t length() <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> @property
{
<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> elements.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;">
As a design decision,<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;">push()</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;">pop()</code><span class="Apple-converted-space">&nbsp;</span>are
defined as regular member functions, 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;">.top</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;">.length</code><span class="Apple-converted-space">&nbsp;</span>are
defined as properties because they can be seen as providing simple informations
about the stack collection.</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;">
Here is 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;">unittest</code><span class="Apple-converted-space">&nbsp;</span>block
for this class that uses its<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>instantiation:</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;">unittest</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> stack = <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> Stack<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>;
<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 newly added element must appear on top
</span> stack.push(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;">assert</span>(stack.top == 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;">assert</span>(stack.length == 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;">// .top and .length should not affect the elements
</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>(stack.top == 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;">assert</span>(stack.length == 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 newly added element must appear on top
</span> stack.push(100);
<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>(stack.top == 100);
<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>(stack.length == 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;">// Removing the last element must expose the previous one
</span> stack.pop();
<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>(stack.top == 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;">assert</span>(stack.length == 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 stack must become empty when the last element is
</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;">// removed
</span> stack.pop();
<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>(stack.length == 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;">
To take advantage of this class template, let&#39;s try using it this time with a
user-defined type. As an example, here is a modified 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;">Point</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> Point(T)
{
T x;
T y;
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;(%s,%s)&quot;</span>, x, 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;">
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;">Stack</code><span class="Apple-converted-space">&nbsp;</span>that
contains elements 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;">Point!double</code><span class="Apple-converted-space">&nbsp;</span>can
be defined like 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;">auto</span> points = <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> Stack!(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>);
</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;">
Here is a test program that first adds ten elements to this stack and then
removes them one by 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;">import</span> std.string;
<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;">import</span> std.random;
<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)
{
T x;
T y;
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;(%s,%s)&quot;</span>, x, y);
}
}
<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;">// Returns a random value between -0.50 and 0.50.
</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> random_double()
<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;">out</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;">assert</span>((result &gt;= -0.50) &amp;&amp; (result &lt; 0.50));
}
<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;">body</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> (<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;">cast</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>)uniform(0, 100) - 50) / 100;
}
<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;">// Returns a Stack that contains &#39;count&#39; number of random
// Point!double elements.
</span>Stack!(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>) randomPoints(size_t 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;">out</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;">assert</span>(result.length == 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;">body</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> points = <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> Stack!(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>);
<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; 0 .. 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;">immutable</span> point = 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>(random_double(),
random_double());
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;adding : &quot;</span>, point);
points.push(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;">return</span> points;
}
<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> stackedPoints = randomPoints(10);
<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;">while</span> (stackedPoints.length) {
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;removing: &quot;</span>, stackedPoints.top);
stackedPoints.pop();
}
}
</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 the output of the program shows, the elements are removed in the reverse
order as they have been added:</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;">adding : (-0.02,-0.01)
adding : (0.17,-0.5)
adding : (0.12,0.23)
adding : (-0.05,-0.47)
adding : (-0.19,-0.11)
adding : (0.42,-0.32)
adding : (0.48,-0.49)
adding : (0.35,0.38)
adding : (-0.2,-0.32)
adding : (0.34,0.27)
removing: (0.34,0.27)
removing: (-0.2,-0.32)
removing: (0.35,0.38)
removing: (0.48,-0.49)
removing: (0.42,-0.32)
removing: (-0.19,-0.11)
removing: (-0.05,-0.47)
removing: (0.12,0.23)
removing: (0.17,-0.5)
removing: (-0.02,-0.01)
</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;">
Function template example: binary search algorithm</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;">
Binary search is the fastest algorithm to search for an element among the
elements of an already sorted array. It is a very simple algorithm: The element
in the middle is considered; if that element is the one that has been sought,
then the search is over. If not, then the algorithm is repeated on the elements
that are either on the left-hand side or on the right-hand side of the middle
element, depending on whether the sought element is greater or less than the
middle element.</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;">
Algorithms that repeat themselves on a smaller range of the initial elements are
recursive. Let&#39;s write the binary search algorithm recursively by calling
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;">
Before converting it to a template, let&#39;s first write this function to support
only arrays 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>.
We can easily convert it to a template later, by adding a template parameter
list and replacing appropriate<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>s
in its definition 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;">T</code>s.
Here is a binary search algorithm that works on arrays 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>:</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;">/*
* This function returns the index of the value if it exists
* in the array, size_t.max otherwise.
*/</span>
size_t binarySearch(<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;">int</span>[] values, <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;">in</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> 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;">// The value is not in the array if the array is empty.
</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> (values.length == 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> size_t.max;
}
<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> midPoint = values.length / 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;">if</span> (value == values[midPoint]) {
<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;">// Found.
</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> midPoint;
} <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: blue; background-position: initial initial; background-repeat: initial initial;">if</span> (value &lt; values[midPoint]) {
<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 value can only be in the left-hand side; let&#39;s
</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;">// search in a slice that represents that half.
</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> binarySearch(values[0 .. midPoint], 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;">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 value can only be in the right-hand side; let&#39;s
</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;">// search in the right-hand side.
</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> index =
binarySearch(values[midPoint + 1 .. $], 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;">if</span> (index != size_t.max) {
<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;">// Adjust the index; it is 0-based in the
</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;">// right-hand side slice.
</span> index += midPoint + 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;">return</span> index;
}
<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;">false</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;We should have never gotten to this line&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 function above implements this simple algorithm in four steps:</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;">
If the array is empty, return<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.max</code><span class="Apple-converted-space">&nbsp;</span>to
indicate that the value has not been found.</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;">
If the element at the mid-point is equal to the sought value, then return
the index of that element.</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;">
If the value is less than the element at the mid-point, then repeat the same
algorithm on the left-hand side.</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;">
Else, repeat the same algorithm on the right-hand side.</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;">
Here is a unittest block that tests the function:</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;">unittest</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> array = [ 1, 2, 3, 5 ];
<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>(binarySearch(array, 0) == size_t.max);
<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>(binarySearch(array, 1) == 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;">assert</span>(binarySearch(array, 4) == size_t.max);
<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>(binarySearch(array, 5) == 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;">assert</span>(binarySearch(array, 6) == size_t.max);
}
</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 that the function has been implemented and tested 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>,
we can convert it to 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;">int</code><span class="Apple-converted-space">&nbsp;</span>appears
only in two places in the definition 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;">size_t binarySearch(<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;">int</span>[] values, <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;">in</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> 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;">// ... int does not appear here ...
</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<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>s
that appear in the parameter list are the types of the elements and the value.
Specifying those as template parameters is sufficient to make this algorithm a
template and to be usable with other types 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;">size_t binarySearch<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;">const</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</span>[] values, <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;">in</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</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;">// ...
</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 can be used with any type that matches the operations
that are applied to that type in the template. In<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;">binarySearch()</code>,
the elements are used only with comparison operators<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>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;">&lt;</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;">if</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;">==</span> values[midPoint]) {
<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: blue; background-position: initial initial; background-repeat: initial initial;">if</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;">&lt;</span> values[midPoint]) {
<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;">
For that reason,<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;">Point</code><span class="Apple-converted-space">&nbsp;</span>is
not ready to be used 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;">binarySearch()</code>yet:</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)
{
T x;
T y;
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;(%s,%s)&quot;</span>, x, y);
}
}
<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()
{
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>[] points;
<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; 0 .. 15) {
points ~= 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>(i, 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;">assert</span>(binarySearch(points, 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>(10, 10)) == 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;">
The program above would cause a compilation error:</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: need member function <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;">opCmp()</span> for struct
const(Point!(int)) to compare
</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;">
According to the error message,<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;">opCmp()</code><span class="Apple-converted-space">&nbsp;</span>needs
to be defined 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;">Point</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;">opCmp()</code><span class="Apple-converted-space">&nbsp;</span>has
been covered in<span class="Apple-converted-space">&nbsp;</span><a href="http://ddili.org/ders/d.en/operator_overloading.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
Operator Overloading 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;">struct</span> Point(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>
<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> opCmp(<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;">ref</span> Point that) <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> (x == that.x
? y - that.y
: x - that.x);
}
}
</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;">
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;">
We will see other features of templates in a later chapter. For a complete
reference on D templates, see<span class="Apple-converted-space">&nbsp;</span><a href="https://github.com/PhilippeSigaud/D-templates-tutorial" 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;">Philippe
Sigaud&#39;s<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;">D
Templates: A Tutorial</i></a>. The following are what we have covered in this
chapter:</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.<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;">void</span> functionTemplate<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 functionParameter)
{
<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;">class</span> ClassTemplate<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: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>}
</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;">
Template arguments can be specified explicitly after an exclamation mark.
The parenteses are not necessary when there is only one token inside the
parenteses.<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;">auto</span> object1 = <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> ClassTemplate!(<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;">auto</span> object2 = <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> ClassTemplate!<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: green; background-position: initial initial; background-repeat: initial initial;">// same thing
</span></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;">
Every different instantiation of a template is a different type.<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;">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;">typeid</span>(ClassTemplate!<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>) !=
<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;">typeid</span>(ClassTemplate!<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>));
</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;">
Template arguments can only be deduced for function templates.<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;"> functionTemplate(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;">// functionTemplate!int(42) is deduced
</span></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;">
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.<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;">class</span> ClassTemplate(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;">dchar</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;">// ...
</span>}
</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;">
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.<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;">void</span> functionTemplate(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;">long</span></span>)(T functionParameter)
{
<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>
</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="invariant.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="alias.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

搜索帮助