<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: 14.1 &#8212; Function templates</title>
	<atom:link href="http://www.learncpp.com/cpp-tutorial/141-function-templates/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learncpp.com/cpp-tutorial/141-function-templates/</link>
	<description></description>
	<lastBuildDate>Wed, 08 Sep 2010 10:51:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Clarisse</title>
		<link>http://www.learncpp.com/cpp-tutorial/141-function-templates/comment-page-1/#comment-85934</link>
		<dc:creator>Clarisse</dc:creator>
		<pubDate>Fri, 04 Jun 2010 15:32:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=200#comment-85934</guid>
		<description>The person who writes n this blog must be really happy with happy feelings =)</description>
		<content:encoded><![CDATA[<p>The person who writes n this blog must be really happy with happy feelings =)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Slice</title>
		<link>http://www.learncpp.com/cpp-tutorial/141-function-templates/comment-page-1/#comment-84505</link>
		<dc:creator>Slice</dc:creator>
		<pubDate>Wed, 12 May 2010 17:04:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=200#comment-84505</guid>
		<description>I&#039;m running Microsoft Visual Studio 2008 Professional Edition, Version 9.0.21022.8 RTM with .NET Framework, Version 3.5 SP1, and I can confirm that it still doesn&#039;t allow you to split a template into the typical header and source file arrangement. In order to get templates to work in that environment, one must still code the entire template definition inside the header file :(.

On the up side: my 1st linker error in the whole tutorial. W00!

Oh, and great tutorial Alex (if you still read these). I even stopped reading the book I was reading on C++ about halfway through and totally moved onto this and even started doing some graphics work at the same time because the information presented here was so clear and concise albeit littered with &quot;it&#039;s&quot; when they should have been &quot;its&quot; :P. I will go back and finish the book at some point just in case I missed anything, but in the mean time onward I go.</description>
		<content:encoded><![CDATA[<p>I&#8217;m running Microsoft Visual Studio 2008 Professional Edition, Version 9.0.21022.8 RTM with .NET Framework, Version 3.5 SP1, and I can confirm that it still doesn&#8217;t allow you to split a template into the typical header and source file arrangement. In order to get templates to work in that environment, one must still code the entire template definition inside the header file :(.</p>
<p>On the up side: my 1st linker error in the whole tutorial. W00!</p>
<p>Oh, and great tutorial Alex (if you still read these). I even stopped reading the book I was reading on C++ about halfway through and totally moved onto this and even started doing some graphics work at the same time because the information presented here was so clear and concise albeit littered with &#8220;it&#8217;s&#8221; when they should have been &#8220;its&#8221; :P. I will go back and finish the book at some point just in case I missed anything, but in the mean time onward I go.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick</title>
		<link>http://www.learncpp.com/cpp-tutorial/141-function-templates/comment-page-1/#comment-79327</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Sun, 28 Feb 2010 13:47:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=200#comment-79327</guid>
		<description>I found your commend in chapter 14.3

&lt;pre&gt;
A note for users using older compilers

Some older compilers (eg. Visual Studio 6) have a bug where the definition of template class functions must be put in the same file as the template class is defined in. Thus, if the template class were defined in X.h, the function definitions would have to also go in X.h (not X.cpp). This issue should be fixed in most/all modern compilers.
&lt;/pre&gt;

I am afraid this is still the case with Visual Studio 2008; if I just completely define the function in the .h file, it works as expected.</description>
		<content:encoded><![CDATA[<p>I found your commend in chapter 14.3</p>
<pre>
A note for users using older compilers

Some older compilers (eg. Visual Studio 6) have a bug where the definition of template class functions must be put in the same file as the template class is defined in. Thus, if the template class were defined in X.h, the function definitions would have to also go in X.h (not X.cpp). This issue should be fixed in most/all modern compilers.
</pre>
<p>I am afraid this is still the case with Visual Studio 2008; if I just completely define the function in the .h file, it works as expected.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick</title>
		<link>http://www.learncpp.com/cpp-tutorial/141-function-templates/comment-page-1/#comment-79326</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Sun, 28 Feb 2010 13:42:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=200#comment-79326</guid>
		<description>Can you define these Template functions in seperate cpp files and have the definition in a .h file like normal functions?

&lt;pre&gt;
// .h file def
template &lt;typename T&gt;
int GetArrayLength(T iArray[]);
&lt;/pre&gt;
&lt;pre&gt;
// .cpp file function
template &lt;typename T&gt; // this is the template parameter declaration
int GetArrayLength(T iArray[])
{
	return sizeof(iArray) / sizeof(iArray[0]);
}
&lt;/pre&gt;
If i then use the GetArrayLength() function from my main.cpp, I get the following build errors:
&lt;pre&gt;
Error	1	error LNK2019: unresolved external symbol &quot;int __cdecl GetArrayLength&lt;int&gt;(int * const)&quot; (??$GetArrayLength@H@@YAHQAH@Z) referenced in function _wmain	CPPTest.obj	CPPTest
Error	2	fatal error LNK1120: 1 unresolved externals	CPPTest.exe	1	CPPTest
&lt;/pre&gt;

If I just define the function twice (once in my helper.cpp and once in my main.cpp), everything works as expected. Any idea what&#039;s wrong here?</description>
		<content:encoded><![CDATA[<p>Can you define these Template functions in seperate cpp files and have the definition in a .h file like normal functions?</p>
<pre>
// .h file def
template &lt;typename T&gt;
int GetArrayLength(T iArray[]);
</pre>
<pre>
// .cpp file function
template &lt;typename T&gt; // this is the template parameter declaration
int GetArrayLength(T iArray[])
{
	return sizeof(iArray) / sizeof(iArray[0]);
}
</pre>
<p>If i then use the GetArrayLength() function from my main.cpp, I get the following build errors:</p>
<pre>
Error	1	error LNK2019: unresolved external symbol &quot;int __cdecl GetArrayLength&lt;int&gt;(int * const)&quot; (??$GetArrayLength@H@@YAHQAH@Z) referenced in function _wmain	CPPTest.obj	CPPTest
Error	2	fatal error LNK1120: 1 unresolved externals	CPPTest.exe	1	CPPTest
</pre>
<p>If I just define the function twice (once in my helper.cpp and once in my main.cpp), everything works as expected. Any idea what&#8217;s wrong here?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: karmeloz</title>
		<link>http://www.learncpp.com/cpp-tutorial/141-function-templates/comment-page-1/#comment-72064</link>
		<dc:creator>karmeloz</dc:creator>
		<pubDate>Wed, 11 Nov 2009 07:11:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=200#comment-72064</guid>
		<description>when we use the keyword &quot;template&quot;, we tell the compiler that what follows is going to be a list of template parameters.
so why do we need the additional keyword typename (or class) for each parameter ? is the keyword typename redudantant 
or does it have a specific function? thanks, Karmeloz</description>
		<content:encoded><![CDATA[<p>when we use the keyword &#8220;template&#8221;, we tell the compiler that what follows is going to be a list of template parameters.<br />
so why do we need the additional keyword typename (or class) for each parameter ? is the keyword typename redudantant<br />
or does it have a specific function? thanks, Karmeloz</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Puneet</title>
		<link>http://www.learncpp.com/cpp-tutorial/141-function-templates/comment-page-1/#comment-69746</link>
		<dc:creator>Puneet</dc:creator>
		<pubDate>Sun, 04 Oct 2009 06:56:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=200#comment-69746</guid>
		<description>&lt;pre&gt;Many people are not able to explain things in such a clear way.. This is really helpful.. Thanks... &lt;/pre&gt;</description>
		<content:encoded><![CDATA[<pre>Many people are not able to explain things in such a clear way.. This is really helpful.. Thanks... </pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Unknown</title>
		<link>http://www.learncpp.com/cpp-tutorial/141-function-templates/comment-page-1/#comment-51423</link>
		<dc:creator>Unknown</dc:creator>
		<pubDate>Wed, 18 Mar 2009 04:59:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=200#comment-51423</guid>
		<description>Have you guys realized that once you declare &quot;using namespace std;&quot; in the beginning of your code....There will be multiple errors in the code if you try to use the template.

For example:
&lt;pre&gt;
using namespace std;

template  // this is the template parameter declaration
Type max(Type tX, Type tY)
{
    return (tX &gt; tY) ? tX : tY;
}

int main()
{
	int nValue = max(3, 7); // returns 7
	double dValue = max(6.34, 18.523); // returns 18.523
	char chValue = max(&#039;a&#039;, &#039;6&#039;); // returns &#039;a&#039;
	cout &lt;&lt; nValue &lt;&lt; &quot; &quot; &lt;&lt; dValue &lt;&lt; &quot; &quot; &lt;&lt; chValue &lt;&lt; endl;
	return 0;
}
&lt;!--formatted--&gt;&lt;/pre&gt;
This creates an error while this doesn&#039;t:
&lt;pre&gt;
template  // this is the template parameter declaration
Type max(Type tX, Type tY)
{
    return (tX &gt; tY) ? tX : tY;
}

int main()
{
	int nValue = max(3, 7); // returns 7
	double dValue = max(6.34, 18.523); // returns 18.523
	char chValue = max(&#039;a&#039;, &#039;6&#039;); // returns &#039;a&#039;
        using namespace std;
	cout &lt;&lt; nValue &lt;&lt; &quot; &quot; &lt;&lt; dValue &lt;&lt; &quot; &quot; &lt;&lt; chValue &lt;&lt; endl;
	return 0;
}
&lt;!--formatted--&gt;&lt;/pre&gt;

[ This is because the local version of max() has a namespace collision with std::max().  I made a note of it at the bottom of the article.  Thanks for the heads up.  -Alex ]</description>
		<content:encoded><![CDATA[<p>Have you guys realized that once you declare &#8220;using namespace std;&#8221; in the beginning of your code&#8230;.There will be multiple errors in the code if you try to use the template.</p>
<p>For example:</p>
<pre>
using namespace std;

template  // this is the template parameter declaration
Type max(Type tX, Type tY)
{
    return (tX &gt; tY) ? tX : tY;
}

int main()
{
	int nValue = max(3, 7); // returns 7
	double dValue = max(6.34, 18.523); // returns 18.523
	char chValue = max('a', '6'); // returns 'a'
	cout &lt;&lt; nValue &lt;&lt; " " &lt;&lt; dValue &lt;&lt; " " &lt;&lt; chValue &lt;&lt; endl;
	return 0;
}
<!--formatted--></pre>
<p>This creates an error while this doesn&#8217;t:</p>
<pre>
template  // this is the template parameter declaration
Type max(Type tX, Type tY)
{
    return (tX &gt; tY) ? tX : tY;
}

int main()
{
	int nValue = max(3, 7); // returns 7
	double dValue = max(6.34, 18.523); // returns 18.523
	char chValue = max('a', '6'); // returns 'a'
        using namespace std;
	cout &lt;&lt; nValue &lt;&lt; " " &lt;&lt; dValue &lt;&lt; " " &lt;&lt; chValue &lt;&lt; endl;
	return 0;
}
<!--formatted--></pre>
<p>[ This is because the local version of max() has a namespace collision with std::max().  I made a note of it at the bottom of the article.  Thanks for the heads up.  -Alex ]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Abhijit Yelegaonkar</title>
		<link>http://www.learncpp.com/cpp-tutorial/141-function-templates/comment-page-1/#comment-32052</link>
		<dc:creator>Abhijit Yelegaonkar</dc:creator>
		<pubDate>Thu, 13 Nov 2008 13:01:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=200#comment-32052</guid>
		<description>&lt;pre&gt; Alex, We will always be in awe of u for this excellent tutorial...God Bless u...&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<pre> Alex, We will always be in awe of u for this excellent tutorial...God Bless u...</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: mohammad</title>
		<link>http://www.learncpp.com/cpp-tutorial/141-function-templates/comment-page-1/#comment-30265</link>
		<dc:creator>mohammad</dc:creator>
		<pubDate>Sun, 19 Oct 2008 16:04:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=200#comment-30265</guid>
		<description>2nd code part:

&lt;pre&gt;
int max(double dX, double dY)   
{   
    return (dX &gt; dY) ? dX : dY;   
}  
&lt;!--formatted--&gt;&lt;/pre&gt;

the return should be a double not an int, so:

&lt;pre&gt;
double max(double dX, double dY)   
{   
    return (dX &gt; dY) ? dX : dY;   
}  
&lt;!--formatted--&gt;&lt;/pre&gt;

is better

[ Yes, thank you.  It&#039;s fixed now. -Alex ]</description>
		<content:encoded><![CDATA[<p>2nd code part:</p>
<pre>
int max(double dX, double dY)
{
    return (dX &gt; dY) ? dX : dY;
}
<!--formatted--></pre>
<p>the return should be a double not an int, so:</p>
<pre>
double max(double dX, double dY)
{
    return (dX &gt; dY) ? dX : dY;
}
<!--formatted--></pre>
<p>is better</p>
<p>[ Yes, thank you.  It's fixed now. -Alex ]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Learn C++ - &#187; 14.3 &#8212; Template classes</title>
		<link>http://www.learncpp.com/cpp-tutorial/141-function-templates/comment-page-1/#comment-19164</link>
		<dc:creator>Learn C++ - &#187; 14.3 &#8212; Template classes</dc:creator>
		<pubDate>Tue, 17 Jun 2008 04:04:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=200#comment-19164</guid>
		<description>[...] 14.1 &#8212; Function templates  [...]</description>
		<content:encoded><![CDATA[<p>[...] 14.1 &#8212; Function templates  [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
