<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
template [C++ Reference]
</title>
<meta name="generator" content="DokuWiki Release 2009-12-25c &quot;Lemming&quot;" />
<meta name="robots" content="index,follow" />
<meta name="date" content="2010-03-21T03:31:55-0700" />
<meta name="keywords" content="keywords,template" />
<link rel="search" type="application/opensearchdescription+xml" href="/wiki/lib/exe/opensearch.php" title="C++ Reference" />
<link rel="start" href="/wiki/" />
<link rel="contents" href="/wiki/keywords/template?do=index" title="Index" />
<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="/wiki/feed.php" />
<link rel="alternate" type="application/rss+xml" title="Current Namespace" href="/wiki/feed.php?mode=list&amp;ns=keywords" />
<link rel="edit" title="Edit this page" href="/wiki/keywords/template?do=edit" />
<link rel="alternate" type="text/html" title="Plain HTML" href="/wiki/_export/xhtml/keywords/template" />
<link rel="alternate" type="text/plain" title="Wiki Markup" href="/wiki/_export/raw/keywords/template" />
<link rel="stylesheet" media="all" type="text/css" href="/wiki/lib/exe/css.php?s=all&amp;t=custom1&amp;tseed=1272971091" />
<link rel="stylesheet" media="screen" type="text/css" href="/wiki/lib/exe/css.php?t=custom1&amp;tseed=1272971091" />
<link rel="stylesheet" media="print" type="text/css" href="/wiki/lib/exe/css.php?s=print&amp;t=custom1&amp;tseed=1272971091" />
<script type="text/javascript" charset="utf-8" ><!--//--><![CDATA[//><!--
var NS='keywords';var JSINFO = {"id":"keywords:template","namespace":"keywords"};
//--><!]]></script>
<script type="text/javascript" charset="utf-8" src="/wiki/lib/exe/js.php?tseed=1272971091" ></script>
<link rel="shortcut icon" href="/wiki/lib/tpl/custom1/images/favicon.png" />
</head>
<body>
<div class="dokuwiki">
<div class="stylehead">
<div class="header">
<div class="pagename">
[[<a href="../keywords/template.html" title="Backlinks">template</a>]]
</div>
<div class="logo">
<a href="http://www.cppreference.com" name="dokuwiki__top" id="dokuwiki__top" accesskey="h" title="[ALT+H]">C++ Reference</a> </div>
<div class="clearer"></div>
</div>
<div class="breadcrumbs">
<span class="bchead">You are here: </span><a href="../start.html" title="start">C++ Reference</a> &raquo; <a href="../keywords/start.html" title="keywords:start">C++ Keywords</a> &raquo; <a href="../keywords/template.html" title="keywords:template">template</a> </div>
</div>
<div class="plugin_translation"><span>Translations of this page<sup><a href="../localization.html" class="wikilink1" title="localization">?</a></sup>:</span> <ul> <li><div class="li"><span class="curid"><a href="../keywords/template.html" class="wikilink1" title="keywords:template">en</a></span></div></li> <li><div class="li"><a href="../br-pt/keywords/template.html" class="wikilink2" title="br-pt:keywords:template" rel="nofollow">br-pt</a></div></li> <li><div class="li"><a href="../cn/keywords/template.html" class="wikilink2" title="cn:keywords:template" rel="nofollow">cn</a></div></li> <li><div class="li"><a href="../cz/keywords/template.html" class="wikilink2" title="cz:keywords:template" rel="nofollow">cz</a></div></li> <li><div class="li"><a href="../de/keywords/template.html" class="wikilink2" title="de:keywords:template" rel="nofollow">de</a></div></li> <li><div class="li"><a href="../es/keywords/template.html" class="wikilink2" title="es:keywords:template" rel="nofollow">es</a></div></li> <li><div class="li"><a href="../fr/keywords/template.html" class="wikilink2" title="fr:keywords:template" rel="nofollow">fr</a></div></li> <li><div class="li"><a href="../it/keywords/template.html" class="wikilink2" title="it:keywords:template" rel="nofollow">it</a></div></li> <li><div class="li"><a href="../jp/keywords/template.html" class="wikilink1" title="jp:keywords:template">jp</a></div></li> <li><div class="li"><a href="../nl/keywords/template.html" class="wikilink2" title="nl:keywords:template" rel="nofollow">nl</a></div></li> <li><div class="li"><a href="../pl/keywords/template.html" class="wikilink2" title="pl:keywords:template" rel="nofollow">pl</a></div></li> <li><div class="li"><a href="../ro/keywords/template.html" class="wikilink2" title="ro:keywords:template" rel="nofollow">ro</a></div></li> <li><div class="li"><a href="../ru/keywords/template.html" class="wikilink2" title="ru:keywords:template" rel="nofollow">ru</a></div></li> <li><div class="li"><a href="../sk/keywords/template.html" class="wikilink2" title="sk:keywords:template" rel="nofollow">sk</a></div></li> <li><div class="li"><a href="../tr/keywords/template.html" class="wikilink2" title="tr:keywords:template" rel="nofollow">tr</a></div></li> <li><div class="li"><a href="../tw/keywords/template.html" class="wikilink2" title="tw:keywords:template" rel="nofollow">tw</a></div></li></ul></div>
<div class="page">
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
<!-- wikipage start -->
<h2><a name="template" id="template">template</a></h2>
<div class="level2">
</div>
<h4><a name="declaring_generic_objects" id="declaring_generic_objects">Declaring Generic Objects</a></h4>
<div class="level4">
<p>
Syntax:
</p>
<pre class="c code c++" style="font-family:monospace;"> template <span class="sy0">&lt;</span>class data<span class="sy0">-</span>type<span class="sy0">&gt;</span> return<span class="sy0">-</span>type name<span class="br0">&#40;</span> parameter<span class="sy0">-</span>list <span class="br0">&#41;</span> <span class="br0">&#123;</span>
statement<span class="sy0">-</span>list<span class="sy0">;</span>
<span class="br0">&#125;</span></pre>
<p>
Templates are used to create generic functions and generic classes and can operate on data without
knowing the nature of that data. They accomplish this by using a placeholder
data-type for which many other <a href="../data_types.html" class="wikilink1" title="data_types">data types</a> can be substituted.
</p>
<p>
For example, the following code uses a template to define a generic swap
function that can swap two variables of any type:
</p>
<pre class="c code c++" style="font-family:monospace;"> template<span class="sy0">&lt;</span>class X<span class="sy0">&gt;</span> <span class="kw4">void</span> genericSwap<span class="br0">&#40;</span> X <span class="sy0">&amp;</span>a<span class="sy0">,</span> X <span class="sy0">&amp;</span>b <span class="br0">&#41;</span> <span class="br0">&#123;</span>
X tmp<span class="sy0">;</span>
&nbsp;
tmp <span class="sy0">=</span> a<span class="sy0">;</span>
a <span class="sy0">=</span> b<span class="sy0">;</span>
b <span class="sy0">=</span> tmp<span class="sy0">;</span>
<span class="br0">&#125;</span>
<span class="kw4">int</span> main<span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
...
<span class="kw4">int</span> num1 <span class="sy0">=</span> <span class="nu0">5</span><span class="sy0">;</span>
<span class="kw4">int</span> num2 <span class="sy0">=</span> <span class="nu0">21</span><span class="sy0">;</span>
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> <span class="sy0">&lt;&lt;</span> <span class="st0">&quot;Before, num1 is &quot;</span> <span class="sy0">&lt;&lt;</span> num1 <span class="sy0">&lt;&lt;</span> <span class="st0">&quot; and num2 is &quot;</span> <span class="sy0">&lt;&lt;</span> num2 <span class="sy0">&lt;&lt;</span> endl<span class="sy0">;</span>
genericSwap<span class="br0">&#40;</span> num1<span class="sy0">,</span> num2 <span class="br0">&#41;</span><span class="sy0">;</span>
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> <span class="sy0">&lt;&lt;</span> <span class="st0">&quot;After, num1 is &quot;</span> <span class="sy0">&lt;&lt;</span> num1 <span class="sy0">&lt;&lt;</span> <span class="st0">&quot; and num2 is &quot;</span> <span class="sy0">&lt;&lt;</span> num2 <span class="sy0">&lt;&lt;</span> endl<span class="sy0">;</span>
<span class="kw4">char</span> c1 <span class="sy0">=</span> <span class="st0">'a'</span><span class="sy0">;</span>
<span class="kw4">char</span> c2 <span class="sy0">=</span> <span class="st0">'z'</span><span class="sy0">;</span>
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> <span class="sy0">&lt;&lt;</span> <span class="st0">&quot;Before, c1 is &quot;</span> <span class="sy0">&lt;&lt;</span> c1 <span class="sy0">&lt;&lt;</span> <span class="st0">&quot; and c2 is &quot;</span> <span class="sy0">&lt;&lt;</span> c2 <span class="sy0">&lt;&lt;</span> endl<span class="sy0">;</span>
genericSwap<span class="br0">&#40;</span> c1<span class="sy0">,</span> c2 <span class="br0">&#41;</span><span class="sy0">;</span>
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> <span class="sy0">&lt;&lt;</span> <span class="st0">&quot;After, c1 is &quot;</span> <span class="sy0">&lt;&lt;</span> c1 <span class="sy0">&lt;&lt;</span> <span class="st0">&quot; and c2 is &quot;</span> <span class="sy0">&lt;&lt;</span> c2 <span class="sy0">&lt;&lt;</span> endl<span class="sy0">;</span>
...
<span class="kw1">return</span><span class="br0">&#40;</span> 0 <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre>
<p>
The next template is used to descibe a generic class:
</p>
<pre class="c code c++" style="font-family:monospace;"><span class="co2">#include &lt;cassert&gt;</span>
&nbsp;
<span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">int</span> maxSize <span class="sy0">=</span> <span class="nu0">20</span><span class="sy0">;</span>
&nbsp;
template<span class="sy0">&lt;</span>class T<span class="sy0">&gt;</span>
class simpleStack
<span class="br0">&#123;</span>
public<span class="sy0">:</span>
simpleStack<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">:</span> amount<span class="br0">&#40;</span>0<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>
bool empty<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> <span class="br0">&#123;</span> <span class="kw1">return</span> amount <span class="sy0">==</span> <span class="nu0">0</span><span class="sy0">;</span> <span class="br0">&#125;</span>
bool full<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> <span class="br0">&#123;</span> <span class="kw1">return</span> amount <span class="sy0">==</span> maxSize<span class="sy0">;</span> <span class="br0">&#125;</span>
<span class="kw4">unsigned</span> <span class="kw4">int</span> size<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> <span class="br0">&#123;</span> <span class="kw1">return</span> amount<span class="sy0">;</span> <span class="br0">&#125;</span>
<span class="kw4">void</span> clear<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> amount <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> <span class="br0">&#125;</span>
<span class="kw4">const</span> T<span class="sy0">&amp;</span> top<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span><span class="sy0">;</span>
<span class="kw4">void</span> pop<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw4">void</span> push<span class="br0">&#40;</span> <span class="kw4">const</span> T <span class="sy0">&amp;</span>x<span class="br0">&#41;</span><span class="sy0">;</span>
private<span class="sy0">:</span>
<span class="kw4">unsigned</span> <span class="kw4">int</span> amount<span class="sy0">;</span>
T array<span class="br0">&#91;</span> maxSize <span class="br0">&#93;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">;</span>
&nbsp;
template<span class="sy0">&lt;</span>class T<span class="sy0">&gt;</span>
<span class="kw4">const</span> T<span class="sy0">&amp;</span> simpleStack<span class="sy0">&lt;</span>T<span class="sy0">&gt;::</span><span class="me2">top</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>
<span class="br0">&#123;</span>
assert<span class="br0">&#40;</span> <span class="sy0">!</span>empty<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw1">return</span> array<span class="br0">&#91;</span> amount <span class="sy0">-</span> 1 <span class="br0">&#93;</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
&nbsp;
template<span class="sy0">&lt;</span>typename T<span class="sy0">&gt;</span> <span class="coMULTI">/*it's allowed and equal to replace class with typename*/</span>
<span class="kw4">void</span> simpleStack<span class="sy0">&lt;</span>T<span class="sy0">&gt;::</span><span class="me2">pop</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
assert<span class="br0">&#40;</span> <span class="sy0">!</span>empty<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="sy0">--</span>amount<span class="sy0">;</span>
<span class="br0">&#125;</span>
&nbsp;
template<span class="sy0">&lt;</span>typename T<span class="sy0">&gt;</span>
<span class="kw4">void</span> simpleStack<span class="sy0">&lt;</span>T<span class="sy0">&gt;::</span><span class="me2">push</span><span class="br0">&#40;</span><span class="kw4">const</span> T <span class="sy0">&amp;</span>x<span class="br0">&#41;</span>
<span class="br0">&#123;</span>
assert<span class="br0">&#40;</span> <span class="sy0">!</span>full<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
array<span class="br0">&#91;</span> amount<span class="sy0">++</span> <span class="br0">&#93;</span> <span class="sy0">=</span> x<span class="sy0">;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="co2">#include &lt;iostream&gt;</span>
&nbsp;
<span class="coMULTI">/* main code */</span>
&nbsp;
<span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
simpleStack<span class="sy0">&lt;</span> <span class="kw4">int</span> <span class="sy0">&gt;</span> aIntStack<span class="sy0">;</span>
&nbsp;
<span class="kw4">int</span> i <span class="sy0">=</span> <span class="nu0">100</span><span class="sy0">;</span>
<span class="kw1">while</span> <span class="br0">&#40;</span> <span class="sy0">!</span>aIntStack.<span class="me1">full</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>
aIntStack.<span class="me1">push</span><span class="br0">&#40;</span> i<span class="sy0">++</span> <span class="br0">&#41;</span><span class="sy0">;</span>
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> <span class="sy0">&lt;&lt;</span> <span class="st0">&quot;stack size: &quot;</span> <span class="sy0">&lt;&lt;</span> aIntStack.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">&lt;&lt;</span> endl<span class="sy0">;</span>
<span class="kw1">return</span> <span class="nu0">0</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre>
</div>
<h4><a name="disambiguating_dependent-names" id="disambiguating_dependent-names">Disambiguating Dependent-Names</a></h4>
<div class="level4">
<p>
The <span class="curid"><a href="../keywords/template.html" class="wikilink1" title="keywords:template">template</a></span> keyword is also used to indicate that a <a href="../keywords/dependent_name.html" class="wikilink1" title="keywords:dependent_name">dependent name</a> refers to a template. In contrast to the placement of the <a href="../keywords/typename.html" class="wikilink1" title="keywords:typename">typename</a> keyword, the <span class="curid"><a href="../keywords/template.html" class="wikilink1" title="keywords:template">template</a></span> keyword is placed just before the name of the template member. For example:
</p>
<pre class="c code c++" style="font-family:monospace;"><span class="kw4">struct</span> Foo <span class="br0">&#123;</span>
template<span class="sy0">&lt;</span>typename T<span class="sy0">&gt;</span>
<span class="kw4">struct</span> Member <span class="br0">&#123;</span>
<span class="kw4">typedef</span> <span class="kw4">int</span> type<span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">;</span>
&nbsp;
template<span class="sy0">&lt;</span>class T<span class="sy0">&gt;</span>
<span class="kw4">struct</span> Bar <span class="br0">&#123;</span>
<span class="kw4">typedef</span> typename T<span class="sy0">::</span><span class="me2">template</span> Member<span class="sy0">&lt;</span>int<span class="sy0">&gt;::</span><span class="me2">type</span> bar<span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">;</span>
&nbsp;
<span class="kw4">typedef</span> Bar<span class="sy0">&lt;</span>Foo<span class="sy0">&gt;</span> FooBar<span class="sy0">;</span></pre>
<p>
Related Topics: <a href="../keywords/typename.html" class="wikilink1" title="keywords:typename">typename</a>
</p>
</div>
<div class="secedit"><form class="button btn_secedit" method="post" action="/wiki/keywords/template"><div class="no"><input type="hidden" name="do" value="edit" /><input type="hidden" name="lines" value="2-" /><input type="hidden" name="rev" value="1269167515" /><input type="submit" value="Edit" class="button" title="template" /></div></form></div>
<!-- wikipage stop -->
</div>
<div class="clearer">&nbsp;</div>
<div class="stylefoot">
<div class="meta">
<div class="user">
</div>
<!--
<div class="doc">
keywords/template.txt &middot; Last modified: 03/21/2010 03:31 by 121.45.160.35 </div>
-->
</div>
<div class="bar" id="bar__bottom">
<div class="bar-left" id="bar__bottomleft">
<a href="../keywords/template.html" class="action edit" accesskey="e" rel="nofollow">Edit this page</a> &#149;
<a href="../keywords/template.html" class="action revisions" accesskey="o" rel="nofollow">Old revisions</a> </div>
<div class="bar-right" id="bar__bottomright">
&#149;
&#149;
&#149;
<a href="../keywords/template.html" class="action login" rel="nofollow">Login</a> &#149;
<a href="../keywords/template.html" class="action index" accesskey="x" rel="nofollow">Index</a> &#149;
<a href="../keywords/template.html" class="action recent" accesskey="r" rel="nofollow">Recent changes</a> &#149;
<a href="../feed.php.html" title="Recent changes RSS feed">RSS</a> &#149;
<a href='http://creativecommons.org/licenses/by/3.0/us/' title='Creative Commons license'>cc</a> &#149;
<form action="/wiki/" accept-charset="utf-8" class="search" id="dw__search"><div class="no"><input type="hidden" name="do" value="search" /><input type="text" id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" /><input type="submit" value="Search" class="button" title="Search" /><div id="qsearch__out" class="ajax_qsearch JSpopup"></div></div></form>&nbsp;
</div>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="no"><img src="/wiki/lib/exe/indexer.php?id=keywords%3Atemplate&amp;1273193143" width="1" height="1" alt="" /></div>
</body>
</html>