<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://assela.pathirana.net/index.php?action=history&amp;feed=atom&amp;title=Template%3ACpptutorial_Templates</id>
	<title>Template:Cpptutorial Templates - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://assela.pathirana.net/index.php?action=history&amp;feed=atom&amp;title=Template%3ACpptutorial_Templates"/>
	<link rel="alternate" type="text/html" href="https://assela.pathirana.net/index.php?title=Template:Cpptutorial_Templates&amp;action=history"/>
	<updated>2026-05-13T18:36:04Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.37.0</generator>
	<entry>
		<id>https://assela.pathirana.net/index.php?title=Template:Cpptutorial_Templates&amp;diff=3596&amp;oldid=prev</id>
		<title>Root: 1 revision(s)</title>
		<link rel="alternate" type="text/html" href="https://assela.pathirana.net/index.php?title=Template:Cpptutorial_Templates&amp;diff=3596&amp;oldid=prev"/>
		<updated>2007-06-08T15:04:45Z</updated>

		<summary type="html">&lt;p&gt;1 revision(s)&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 15:04, 8 June 2007&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Root</name></author>
	</entry>
	<entry>
		<id>https://assela.pathirana.net/index.php?title=Template:Cpptutorial_Templates&amp;diff=3595&amp;oldid=prev</id>
		<title>Root at 09:48, 3 April 2007</title>
		<link rel="alternate" type="text/html" href="https://assela.pathirana.net/index.php?title=Template:Cpptutorial_Templates&amp;diff=3595&amp;oldid=prev"/>
		<updated>2007-04-03T09:48:03Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;=Templates=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Function templates===&lt;br /&gt;
&lt;br /&gt;
Function templates are special functions that can operate with &amp;#039;&amp;#039;generic types&amp;#039;&amp;#039;. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.&lt;br /&gt;
&lt;br /&gt;
In C++ this can be achieved using &amp;#039;&amp;#039;template parameters&amp;#039;&amp;#039;. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function. These function templates can use these parameters as if they were any other regular type.&lt;br /&gt;
&lt;br /&gt;
The format for declaring function templates with type parameters is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt; template &amp;lt;class identifier&amp;gt; function_declaration;&amp;lt;br /&amp;gt;template &amp;lt;typename identifier&amp;gt; function_declaration;&amp;lt;br /&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The only difference between both prototypes is the use of either the keyword &amp;lt;tt&amp;gt;class&amp;lt;/tt&amp;gt; or the keyword &amp;lt;tt&amp;gt;typename&amp;lt;/tt&amp;gt;. Its use is indistinct, since both expressions have exactly the same meaning and behave exactly the same way.&lt;br /&gt;
&lt;br /&gt;
For example, to create a template function that returns the greater one of two objects we could use:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;template&amp;lt;/span&amp;gt; &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; myType&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 myType GetMax (myType a, myType b) {&lt;br /&gt;
  &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;return&amp;lt;/span&amp;gt; (a&amp;gt;b?a:b);&lt;br /&gt;
 }&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Here we have created a template function with &amp;lt;tt&amp;gt;myType&amp;lt;/tt&amp;gt; as its template parameter. This template parameter represents a type that has not yet been specified, but that can be used in the template function as if it were a regular type. As you can see, the function template &amp;lt;tt&amp;gt;GetMax&amp;lt;/tt&amp;gt; returns the greater of two parameters of this still-undefined type.&lt;br /&gt;
&lt;br /&gt;
To use this function template we use the following format for the function call:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt; function_name &amp;lt;type&amp;gt; (parameters);&amp;lt;br /&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, to call &amp;lt;tt&amp;gt;GetMax&amp;lt;/tt&amp;gt; to compare two integer values of type &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt; we can write:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; x,y;&lt;br /&gt;
 GetMax &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt;&amp;gt; (x,y);&lt;br /&gt;
 &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
When the compiler encounters this call to a template function, it uses the template to automatically generate a function replacing each appearance of &amp;lt;tt&amp;gt;myType&amp;lt;/tt&amp;gt; by the type passed as the actual template parameter (&amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt; in this case) and then calls it. This process is automatically performed by the compiler and is invisible to the programmer.&lt;br /&gt;
&lt;br /&gt;
Here is the entire example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;codebox&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;comm&amp;quot;&amp;gt;// function template&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span class=&amp;quot;prep&amp;quot;&amp;gt;&amp;lt;nowiki&amp;gt;#include &amp;lt;iostream&amp;gt;&amp;lt;/nowiki&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;using&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;namespace&amp;lt;/span&amp;gt; std;&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;template&amp;lt;/span&amp;gt; &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; T&amp;gt;&lt;br /&gt;
 T GetMax (T a, T b) {&lt;br /&gt;
   T result;&lt;br /&gt;
   result = (a&amp;gt;b)? a : b;&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;return&amp;lt;/span&amp;gt; (result);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; main () {&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; i=5, j=6, k;&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;long&amp;lt;/span&amp;gt; l=10, m=5, n;&lt;br /&gt;
   k=GetMax&amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt;&amp;gt;(i,j);&lt;br /&gt;
   n=GetMax&amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;long&amp;lt;/span&amp;gt;&amp;gt;(l,m);&lt;br /&gt;
   cout &amp;lt;&amp;lt; k &amp;lt;&amp;lt; endl;&lt;br /&gt;
   cout &amp;lt;&amp;lt; n &amp;lt;&amp;lt; endl;&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;return&amp;lt;/span&amp;gt; 0;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
| class=&amp;quot;result&amp;quot; |&lt;br /&gt;
 6&lt;br /&gt;
 10&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In this case, we have used &amp;lt;tt&amp;gt;T&amp;lt;/tt&amp;gt; as the template parameter name instead of &amp;lt;tt&amp;gt;myType&amp;lt;/tt&amp;gt; because it is shorter and in fact is a very common template parameter name. But you can use any identifier you like.&lt;br /&gt;
&lt;br /&gt;
In the example above we used the function template &amp;lt;tt&amp;gt;GetMax()&amp;lt;/tt&amp;gt; twice. The first time with arguments of type &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt; and the second one with arguments of type &amp;lt;tt&amp;gt;long&amp;lt;/tt&amp;gt;. The compiler has instantiated and then called each time the appropriate version of the function.&lt;br /&gt;
&lt;br /&gt;
As you can see, the type &amp;lt;tt&amp;gt;T&amp;lt;/tt&amp;gt; is used within the &amp;lt;tt&amp;gt;GetMax()&amp;lt;/tt&amp;gt; template function even to declare new objects of that type:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 T result;&lt;br /&gt;
 &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Therefore, &amp;lt;tt&amp;gt;result&amp;lt;/tt&amp;gt; will be an object of the same type as the parameters &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;b&amp;lt;/tt&amp;gt; when the function template is instantiated with a specific type.&lt;br /&gt;
&lt;br /&gt;
In this specific case where the generic type &amp;lt;tt&amp;gt;T&amp;lt;/tt&amp;gt; is used as a parameter for &amp;lt;tt&amp;gt;GetMax&amp;lt;/tt&amp;gt; the compiler can find out automatically which data type has to instantiate without having to explicitly specify it within angle brackets (like we have done before specifying &amp;lt;tt&amp;gt;&amp;lt;int&amp;gt;&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;&amp;lt;long&amp;gt;&amp;lt;/tt&amp;gt;). So we could have written instead:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; i,j;&lt;br /&gt;
 GetMax (i,j);&lt;br /&gt;
 &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Since both &amp;lt;tt&amp;gt;i&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;j&amp;lt;/tt&amp;gt; are of type &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;, and the compiler can automatically find out that the template parameter can only be &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;. This implicit method produces exactly the same result:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;codebox&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;comm&amp;quot;&amp;gt;// function template II&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span class=&amp;quot;prep&amp;quot;&amp;gt;&amp;lt;nowiki&amp;gt;#include &amp;lt;iostream&amp;gt;&amp;lt;/nowiki&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;using&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;namespace&amp;lt;/span&amp;gt; std;&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;template&amp;lt;/span&amp;gt; &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; T&amp;gt;&lt;br /&gt;
 T GetMax (T a, T b) {&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;return&amp;lt;/span&amp;gt; (a&amp;gt;b?a:b);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; main () {&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; i=5, j=6, k;&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;long&amp;lt;/span&amp;gt; l=10, m=5, n;&lt;br /&gt;
   k=GetMax(i,j);&lt;br /&gt;
   n=GetMax(l,m);&lt;br /&gt;
   cout &amp;lt;&amp;lt; k &amp;lt;&amp;lt; endl;&lt;br /&gt;
   cout &amp;lt;&amp;lt; n &amp;lt;&amp;lt; endl;&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;return&amp;lt;/span&amp;gt; 0;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
| class=&amp;quot;result&amp;quot; |&lt;br /&gt;
 6&lt;br /&gt;
 10&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice how in this case, we called our function template &amp;lt;tt&amp;gt;GetMax()&amp;lt;/tt&amp;gt; without explicitly specifying the type between angle-brackets &amp;lt;tt&amp;gt;&amp;lt;&amp;gt;&amp;lt;/tt&amp;gt;. The compiler automatically determines what type is needed on each call.&lt;br /&gt;
&lt;br /&gt;
Because our template function includes only one template parameter (&amp;lt;tt&amp;gt;class T&amp;lt;/tt&amp;gt;) and the function template itself accepts two parameters, both of this &amp;lt;tt&amp;gt;T&amp;lt;/tt&amp;gt; type, we cannot call our function template with two objects of different types as arguments:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; i;&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;long&amp;lt;/span&amp;gt; l;&lt;br /&gt;
 k = GetMax (i,l); &lt;br /&gt;
 &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This would not be correct, since our &amp;lt;tt&amp;gt;GetMax&amp;lt;/tt&amp;gt; function template expects two arguments of the same type, and in this call to it we use objects of two different types.&lt;br /&gt;
&lt;br /&gt;
We can also define function templates that accept more than one type parameter, simply by specifying more template parameters between the angle brackets. For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;template&amp;lt;/span&amp;gt; &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; T, &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; U&amp;gt;&lt;br /&gt;
 T GetMin (T a, U b) {&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;return&amp;lt;/span&amp;gt; (a&amp;lt;b?a:b);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In this case, our function template &amp;lt;tt&amp;gt;GetMin()&amp;lt;/tt&amp;gt; accepts two parameters of different types and returns an object of the same type as the first parameter (&amp;lt;tt&amp;gt;T&amp;lt;/tt&amp;gt;) that is passed. For example, after that declaration we could call &amp;lt;tt&amp;gt;GetMin()&amp;lt;/tt&amp;gt; with:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; i,j;&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;long&amp;lt;/span&amp;gt; l;&lt;br /&gt;
 i = GetMin&amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt;,&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;long&amp;lt;/span&amp;gt;&amp;gt; (j,l);&lt;br /&gt;
 &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
or simply:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 i = GetMin (j,l);&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
even though &amp;lt;tt&amp;gt;j&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;l&amp;lt;/tt&amp;gt; have different types, since the compiler can determine the apropriate instantiation anyway.&lt;br /&gt;
&lt;br /&gt;
===Class templates===&lt;br /&gt;
&lt;br /&gt;
We also have the possibility to write class templates, so that a class can have members that use template parameters as types. For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;template&amp;lt;/span&amp;gt; &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; T&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; mypair {&lt;br /&gt;
     T values [2];&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;public&amp;lt;/span&amp;gt;&amp;lt;nowiki&amp;gt;:&lt;br /&gt;
     mypair (T first, T second)&lt;br /&gt;
     {&lt;br /&gt;
       values[0]=first; values[1]=second;&lt;br /&gt;
     }&lt;br /&gt;
 };&lt;br /&gt;
 &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The class that we have just defined serves to store two elements of any valid type. For example, if we wanted to declare an object of this class to store two integer values of type &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt; with the values 115 and 36 we would write:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 mypair&amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt;&amp;gt; myobject (115, 36);&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
this same class would also be used to create an object to store any other type:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 mypair&amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;double&amp;lt;/span&amp;gt;&amp;gt; myfloats (3.0, 2.18); &lt;br /&gt;
 &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The only member function in the previous class template has been defined inline within the class declaration itself. In case that we define a function member outside the declaration of the class template, we must always precede that definition with the &amp;lt;tt&amp;gt;template &amp;lt;...&amp;gt;&amp;lt;/tt&amp;gt; prefix:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;codebox&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;comm&amp;quot;&amp;gt;// class templates&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span class=&amp;quot;prep&amp;quot;&amp;gt;&amp;lt;nowiki&amp;gt;#include &amp;lt;iostream&amp;gt;&amp;lt;/nowiki&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;using&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;namespace&amp;lt;/span&amp;gt; std;&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;template&amp;lt;/span&amp;gt; &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; T&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; mypair {&lt;br /&gt;
     T a, b;&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;public&amp;lt;/span&amp;gt;&amp;lt;nowiki&amp;gt;:&lt;br /&gt;
     mypair (T first, T second)&lt;br /&gt;
       {a=first; b=second;}&lt;br /&gt;
     T getmax ();&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;/nowiki&amp;gt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;template&amp;lt;/span&amp;gt; &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; T&amp;gt;&lt;br /&gt;
 T mypair&amp;lt;T&amp;gt;::getmax ()&lt;br /&gt;
 {&lt;br /&gt;
   T retval;&lt;br /&gt;
   retval = a&amp;gt;b? a : b;&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;return&amp;lt;/span&amp;gt; retval;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; main () {&lt;br /&gt;
   mypair &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt;&amp;gt; myobject (100, 75);&lt;br /&gt;
   cout &amp;lt;&amp;lt; myobject.getmax();&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;return&amp;lt;/span&amp;gt; 0;&lt;br /&gt;
 }&lt;br /&gt;
| class=&amp;quot;result&amp;quot; |&lt;br /&gt;
 100&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice the syntax of the definition of member function getmax:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;template&amp;lt;/span&amp;gt; &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; T&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 T mypair&amp;lt;T&amp;gt;::getmax ()&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Confused by so many &amp;lt;tt&amp;gt;T&amp;lt;/tt&amp;gt;&amp;#039;s? There are three &amp;lt;tt&amp;gt;T&amp;lt;/tt&amp;gt;&amp;#039;s in this declaration: The first one is the template parameter. The second &amp;lt;tt&amp;gt;T&amp;lt;/tt&amp;gt; refers to the type returned by the function. And the third &amp;lt;tt&amp;gt;T&amp;lt;/tt&amp;gt; (the one between angle brackets) is also a requirement: It specifies that this function&amp;#039;s template parameter is also the class template parameter.&lt;br /&gt;
&lt;br /&gt;
===Template specialization===&lt;br /&gt;
&lt;br /&gt;
If we want to define a different implementation for a template when a specific type is passed as template parameter, we can declare a specialization of that template.&lt;br /&gt;
&lt;br /&gt;
For example, let&amp;#039;s suppose that we have a very simple class called &amp;lt;tt&amp;gt;mycontainer&amp;lt;/tt&amp;gt; that can store one element of any type and that it has just one member function called &amp;lt;tt&amp;gt;increase&amp;lt;/tt&amp;gt;, which increases its value. But we find that when it stores an element of type &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; it would be more convenient to have a completely different implementation with a function member &amp;lt;tt&amp;gt;uppercase&amp;lt;/tt&amp;gt;, so we decide to declare a class template specialization for that type:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;codebox&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;comm&amp;quot;&amp;gt;// template specialization&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;prep&amp;quot;&amp;gt;&amp;lt;nowiki&amp;gt;#include &amp;lt;iostream&amp;gt;&amp;lt;/nowiki&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;using&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;namespace&amp;lt;/span&amp;gt; std;&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;comm&amp;quot;&amp;gt;// class template:&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;template&amp;lt;/span&amp;gt; &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; T&amp;gt;&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; mycontainer {&lt;br /&gt;
     T element;&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;public&amp;lt;/span&amp;gt;&amp;lt;nowiki&amp;gt;:&lt;br /&gt;
     mycontainer (T arg) {element=arg;}&lt;br /&gt;
     T increase () {&amp;lt;/nowiki&amp;gt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;return&amp;lt;/span&amp;gt; ++element;}&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;comm&amp;quot;&amp;gt;// class template specialization:&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;template&amp;lt;/span&amp;gt; &amp;lt;&amp;gt;&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; mycontainer &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;char&amp;lt;/span&amp;gt;&amp;gt; {&lt;br /&gt;
     &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;char&amp;lt;/span&amp;gt; element;&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;public&amp;lt;/span&amp;gt;&amp;lt;nowiki&amp;gt;:&lt;br /&gt;
     mycontainer (&amp;lt;/nowiki&amp;gt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;char&amp;lt;/span&amp;gt; arg) {element=arg;}&lt;br /&gt;
     &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;char&amp;lt;/span&amp;gt; uppercase ()&lt;br /&gt;
     {&lt;br /&gt;
       &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;if&amp;lt;/span&amp;gt; ((element&amp;gt;=&amp;lt;span class=&amp;quot;str&amp;quot;&amp;gt;&amp;#039;a&amp;#039;&amp;lt;/span&amp;gt;)&amp;amp;&amp;amp;(element&amp;lt;=&amp;lt;span class=&amp;quot;str&amp;quot;&amp;gt;&amp;#039;z&amp;#039;&amp;lt;/span&amp;gt;))&lt;br /&gt;
       element+=&amp;lt;span class=&amp;quot;str&amp;quot;&amp;gt;&amp;#039;A&amp;#039;&amp;lt;/span&amp;gt;-&amp;lt;span class=&amp;quot;str&amp;quot;&amp;gt;&amp;#039;a&amp;#039;&amp;lt;/span&amp;gt;&amp;lt;nowiki&amp;gt;;&lt;br /&gt;
       &amp;lt;/nowiki&amp;gt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;return&amp;lt;/span&amp;gt; element;&lt;br /&gt;
     }&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; main () {&lt;br /&gt;
   mycontainer&amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt;&amp;gt; myint (7);&lt;br /&gt;
   mycontainer&amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;char&amp;lt;/span&amp;gt;&amp;gt; mychar (&amp;lt;span class=&amp;quot;str&amp;quot;&amp;gt;&amp;#039;j&amp;#039;&amp;lt;/span&amp;gt;);&lt;br /&gt;
   cout &amp;lt;&amp;lt; myint.increase() &amp;lt;&amp;lt; endl;&lt;br /&gt;
   cout &amp;lt;&amp;lt; mychar.uppercase() &amp;lt;&amp;lt; endl;&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;return&amp;lt;/span&amp;gt; 0;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
| class=&amp;quot;result&amp;quot; |&lt;br /&gt;
 8&lt;br /&gt;
 J&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is the syntax used in the class template specialization:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;template&amp;lt;/span&amp;gt; &amp;lt;&amp;gt; &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; mycontainer &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;char&amp;lt;/span&amp;gt;&amp;gt; { ... };&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
First of all, notice that we precede the class template name with an empty&amp;lt;tt&amp;gt;template&amp;lt;&amp;gt;&amp;lt;/tt&amp;gt; parameter list. This is to explicitly declare it as a template specialization.&lt;br /&gt;
&lt;br /&gt;
But more important than this prefix, is the &amp;lt;tt&amp;gt;&amp;lt;char&amp;gt;&amp;lt;/tt&amp;gt; specialization parameter after the class template name. This specialization parameter itself identifies the type for which we are going to declare a template class specialization (&amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt;). Notice the differences between the generic class template and the specialization:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;template&amp;lt;/span&amp;gt; &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; T&amp;gt; &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; mycontainer { ... };&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;template&amp;lt;/span&amp;gt; &amp;lt;&amp;gt; &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; mycontainer &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;char&amp;lt;/span&amp;gt;&amp;gt; { ... };&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The first line is the generic template, and the second one is the specialization.&lt;br /&gt;
&lt;br /&gt;
When we declare specializations for a template class, we must also define all its members, even those exactly equal to the generic template class, because there is no &amp;quot;inheritance&amp;quot; of members from the generic template to the specialization.&lt;br /&gt;
&lt;br /&gt;
===Non-type parameters for templates===&lt;br /&gt;
&lt;br /&gt;
Besides the template arguments that are preceded by the &amp;lt;tt&amp;gt;class&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;typename&amp;lt;/tt&amp;gt; keywords , which represent types, templates can also have regular typed parameters, similar to those found in functions. As an example, have a look at this class template that is used to contain sequences of elements:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;codebox&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;comm&amp;quot;&amp;gt;// sequence template&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;prep&amp;quot;&amp;gt;&amp;lt;nowiki&amp;gt;#include &amp;lt;iostream&amp;gt;&amp;lt;/nowiki&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;using&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;namespace&amp;lt;/span&amp;gt; std;&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;template&amp;lt;/span&amp;gt; &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; T, &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; N&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; mysequence {&lt;br /&gt;
     T memblock [N];&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;public&amp;lt;/span&amp;gt;&amp;lt;nowiki&amp;gt;:&lt;br /&gt;
     &amp;lt;/nowiki&amp;gt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;void&amp;lt;/span&amp;gt; setmember (&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; x, T value);&lt;br /&gt;
     T getmember (&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; x);&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;template&amp;lt;/span&amp;gt; &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; T, &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; N&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;void&amp;lt;/span&amp;gt; mysequence&amp;lt;T,N&amp;gt;::setmember (&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; x, T value) {&lt;br /&gt;
   memblock[x]=value;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;template&amp;lt;/span&amp;gt; &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; T, &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; N&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 T mysequence&amp;lt;T,N&amp;gt;::getmember (&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; x) {&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;return&amp;lt;/span&amp;gt; memblock[x];&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; main () {&lt;br /&gt;
   mysequence &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt;,5&amp;gt; myints;&lt;br /&gt;
   mysequence &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;double&amp;lt;/span&amp;gt;,5&amp;gt; myfloats;&lt;br /&gt;
   myints.setmember (0,100);&lt;br /&gt;
   myfloats.setmember (3,3.1416);&lt;br /&gt;
   cout &amp;lt;&amp;lt; myints.getmember(0) &amp;lt;&amp;lt; &amp;lt;span class=&amp;quot;str&amp;quot;&amp;gt;&amp;#039;\n&amp;#039;&amp;lt;/span&amp;gt;&amp;lt;nowiki&amp;gt;;&lt;br /&gt;
   cout &amp;lt;&amp;lt; myfloats.getmember(3) &amp;lt;&amp;lt; &amp;lt;/nowiki&amp;gt;&amp;lt;span class=&amp;quot;str&amp;quot;&amp;gt;&amp;#039;\n&amp;#039;&amp;lt;/span&amp;gt;&amp;lt;nowiki&amp;gt;;&lt;br /&gt;
   &amp;lt;/nowiki&amp;gt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;return&amp;lt;/span&amp;gt; 0;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
| class=&amp;quot;result&amp;quot; |&lt;br /&gt;
 100&lt;br /&gt;
 3.1416&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It is also possible to set default values or types for class template parameters. For example, if the previous class template definition had been:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;template&amp;lt;/span&amp;gt; &amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; T=&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;char&amp;lt;/span&amp;gt;, &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; N=10&amp;gt; &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;class&amp;lt;/span&amp;gt; mysequence {..};&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
We could create objects using the default template parameters by declaring:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 mysequence&amp;lt;&amp;gt; myseq;&lt;br /&gt;
 &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Which would be equivalent to:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 mysequence&amp;lt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;char&amp;lt;/span&amp;gt;,10&amp;gt; myseq;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Templates and multiple-file projects===&lt;br /&gt;
&lt;br /&gt;
From the point of view of the compiler, templates are not normal functions or classes. They are compiled on demand, meaning that the code of a template function is not compiled until an instantiation with specific template arguments is required. At that moment, when an instantiation is required, the compiler generates a function specifically for those arguments from the template.&lt;br /&gt;
&lt;br /&gt;
When projects grow it is usual to split the code of a program in different source code files. In these cases, the interface and implementation are generally separated. Taking a library of functions as example, the interface generally consists of declarations of the prototypes of all the functions that can be called. These are generally declared in a &amp;quot;header file&amp;quot; with a .h extension, and the implementation (the definition of these functions) is in an independent file with c++ code.&lt;br /&gt;
&lt;br /&gt;
Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.&lt;br /&gt;
&lt;br /&gt;
Since no code is generated until a template is instantiated when required, compilers are prepared to allow the inclusion more than once of the same template file with both declarations and definitions in a project without generating linkage errors.&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Root</name></author>
	</entry>
</feed>