<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>what is function template in c++ with example - Wikitechy</title>
	<atom:link href="https://www.wikitechy.com/interview-questions/tag/what-is-function-template-in-c-with-example/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.wikitechy.com/interview-questions/tag/what-is-function-template-in-c-with-example/</link>
	<description>Interview Questions</description>
	<lastBuildDate>Fri, 19 Aug 2022 11:54:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.wikitechy.com/interview-questions/wp-content/uploads/2025/10/cropped-wikitechy-icon-32x32.png</url>
	<title>what is function template in c++ with example - Wikitechy</title>
	<link>https://www.wikitechy.com/interview-questions/tag/what-is-function-template-in-c-with-example/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>What is Template in C++ ?</title>
		<link>https://www.wikitechy.com/interview-questions/cpp/what-is-template-in-c/</link>
					<comments>https://www.wikitechy.com/interview-questions/cpp/what-is-template-in-c/#respond</comments>
		
		<dc:creator><![CDATA[webmaster]]></dc:creator>
		<pubDate>Fri, 19 Aug 2022 11:54:35 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[advantages of templates in c++]]></category>
		<category><![CDATA[class templates in c++]]></category>
		<category><![CDATA[function template in c++ program]]></category>
		<category><![CDATA[function templates in c++]]></category>
		<category><![CDATA[template in c++]]></category>
		<category><![CDATA[template parameters c++]]></category>
		<category><![CDATA[templates in c++]]></category>
		<category><![CDATA[types of templates in c++]]></category>
		<category><![CDATA[what is a template in c programming]]></category>
		<category><![CDATA[what is class template in c language]]></category>
		<category><![CDATA[what is function template in c]]></category>
		<category><![CDATA[what is function template in c plus plus]]></category>
		<category><![CDATA[what is function template in c++ with example]]></category>
		<category><![CDATA[what is template class t in c++]]></category>
		<category><![CDATA[what is template in c plus plus]]></category>
		<category><![CDATA[what is template in c++]]></category>
		<category><![CDATA[what is template in computer]]></category>
		<guid isPermaLink="false">https://www.wikitechy.com/interview-questions/?p=4805</guid>

					<description><![CDATA[In C++ a template is a simple and yet very powerful tool. We don’t need to write the same code for different data types because it is simple idea to pass data type as a parameter. Rather than maintaining and writing the multiple codes, we can write one sort () and pass data type as [&#8230;]]]></description>
										<content:encoded><![CDATA[<ul>
<li style="text-align: justify;">In C++ a template is a simple and yet very powerful tool.</li>
<li style="text-align: justify;">We don’t need to write the same code for different data types because it is simple idea to pass data type as a parameter.</li>
<li style="text-align: justify;">Rather than maintaining and writing the multiple codes, we can write one sort () and pass data type as a parameter.</li>
<li style="text-align: justify;">At compiler time templates are expanded, hence this is like macros.</li>
<li style="text-align: justify;">At C++ there are two types of support templates, they are ‘template’ and ‘typename’.</li>
<li style="text-align: justify;">The difference is, the compiler does type checking before template expansion and the idea is source code contains only function/class, but compiled code may contain multiple copies of same function/class.</li>
<li>Templates can be represented in two ways they are Function templates and Class templates.</li>
</ul>
<p><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-4806 aligncenter" src="https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/templates-C.png" alt="" width="1800" height="835" srcset="https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/templates-C.png 1800w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/templates-C-300x139.png 300w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/templates-C-1024x475.png 1024w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/templates-C-768x356.png 768w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/templates-C-1536x713.png 1536w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/templates-C-390x181.png 390w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/templates-C-820x380.png 820w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/templates-C-1180x547.png 1180w" sizes="(max-width: 1800px) 100vw, 1800px" /></p>
<h2 id="function-template">Function Template</h2>
<ul>
<li>Generic functions use the concept of function template and it defines a set of operations that can be applied to the various types of data.</li>
<li>The function will operate on depends on the type of the data passed as a parameter.</li>
<li>It can be implemented to an array of floats or array of integers, quick sorting algorithm is implemented using a generic function.</li>
<li>Using keyword template generic function is created and template defines what function will do.</li>
</ul>
<h2 id="sample-code">Sample Code</h2>
<div class="code-embed-wrapper"> <div class="code-embed-infos"> </div> <pre class="language-cpp code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-cpp code-embed-code">#include &lt;iostream&gt;  <br/>using namespace std;  <br/>template&lt;class T&gt; T add(T &amp;a,T &amp;b)  <br/>{  <br/>    T result = a+b;  <br/>    return result;  <br/>      <br/>}  <br/>int main()  <br/>{  <br/>  int i =2;  <br/>  int j =3;  <br/>  float m = 2.3;  <br/>  float n = 1.2;  <br/>  cout&lt;&lt;&quot;Addition of i and j is :&quot;&lt;&lt;add(i,j);  <br/>  cout&lt;&lt;&#039;\n&#039;;  <br/>  cout&lt;&lt;&quot;Addition of m and n is :&quot;&lt;&lt;add(m,n);  <br/>  return 0;  <br/>}  </code></pre> </div>
<h2 id="output">Output</h2>
<p><img decoding="async" class="alignnone size-full wp-image-4807" src="https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/function-template.png" alt="" width="1438" height="207" srcset="https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/function-template.png 1438w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/function-template-300x43.png 300w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/function-template-1024x147.png 1024w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/function-template-768x111.png 768w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/function-template-390x56.png 390w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/function-template-820x118.png 820w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/function-template-1180x170.png 1180w" sizes="(max-width: 1438px) 100vw, 1438px" /></p>
<h2 id="class-template">Class Template</h2>
<ul>
<li>Class template is also similar to function template.</li>
<li>If class uses the concept of template, then the class is known as generic class.</li>
<li>In class template Ttype is a placeholder name which will be determined when the class is instantiated.</li>
<li>We can define one or more than generic data type using a comma-separated list. It can be used inside the class body.</li>
</ul>
<h2 id="sample-code-2"><strong>Sample Code</strong></h2>
<div class="code-embed-wrapper"> <div class="code-embed-infos"> </div> <pre class="language-cpp code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-cpp code-embed-code">#include &lt;iostream&gt;  <br/><br/>using namespace std;  <br/><br/>template&lt;class T&gt;  <br/><br/>class A   <br/><br/>{  <br/><br/>    public:  <br/><br/>    T num1 = 5;  <br/><br/>    T num2 = 6;  <br/><br/>    void add()  <br/><br/>    {  <br/><br/>        std::cout &lt;&lt; &quot;Addition of num1 and num2 : &quot; &lt;&lt; num1+num2&lt;&lt;std::endl;  <br/><br/>    }  <br/><br/>      <br/><br/>};  <br/><br/>  <br/><br/>int main()  <br/><br/>{  <br/><br/>    A&lt;int&gt; d;  <br/><br/>    d.add();  <br/><br/>    return 0;  <br/><br/>}  </code></pre> </div>
<h2 id="output-2">Output</h2>
<p><img decoding="async" class="alignnone size-full wp-image-4808" src="https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/class-template.png" alt="" width="1410" height="144" srcset="https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/class-template.png 1410w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/class-template-300x31.png 300w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/class-template-1024x105.png 1024w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/class-template-768x78.png 768w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/class-template-390x40.png 390w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/class-template-820x84.png 820w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/class-template-1180x121.png 1180w" sizes="(max-width: 1410px) 100vw, 1410px" /></p>
<p style="text-align: justify;">
]]></content:encoded>
					
					<wfw:commentRss>https://www.wikitechy.com/interview-questions/cpp/what-is-template-in-c/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
