<?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.2 &#8212; Function template instances</title>
	<atom:link href="http://www.learncpp.com/cpp-tutorial/142-function-template-instances/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learncpp.com/cpp-tutorial/142-function-template-instances/</link>
	<description></description>
	<lastBuildDate>Tue, 07 Feb 2012 12:30:29 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: vamsi</title>
		<link>http://www.learncpp.com/cpp-tutorial/142-function-template-instances/comment-page-1/#comment-93793</link>
		<dc:creator>vamsi</dc:creator>
		<pubDate>Fri, 15 Oct 2010 11:03:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=201#comment-93793</guid>
		<description>&lt;pre&gt;
Prashant. Better to use &#039;typename&#039; for &quot;function template&quot; and &#039;class&#039; for &quot;class template&quot;
&lt;!--formatted--&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<pre>
Prashant. Better to use &#39;typename&#39; for "function template" and &#39;class&#39; for "class template"
<!--formatted--></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Prashant Patel</title>
		<link>http://www.learncpp.com/cpp-tutorial/142-function-template-instances/comment-page-1/#comment-92860</link>
		<dc:creator>Prashant Patel</dc:creator>
		<pubDate>Tue, 28 Sep 2010 11:39:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=201#comment-92860</guid>
		<description>&lt;pre&gt;
what is difference between template &lt;typename T&gt;  and template  &lt;class T&gt;.
Form me both are genrating same result.
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<pre>
what is difference between template &lt;typename T&gt;  and template  &lt;class T&gt;.
Form me both are genrating same result.
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Conrado</title>
		<link>http://www.learncpp.com/cpp-tutorial/142-function-template-instances/comment-page-1/#comment-62871</link>
		<dc:creator>Conrado</dc:creator>
		<pubDate>Mon, 22 Jun 2009 16:48:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=201#comment-62871</guid>
		<description>Read http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/ to understand the use of &quot;friend function&quot; to overload an operator.</description>
		<content:encoded><![CDATA[<p>Read <a href="http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/" rel="nofollow">http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/</a> to understand the use of &#8220;friend function&#8221; to overload an operator.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: arun</title>
		<link>http://www.learncpp.com/cpp-tutorial/142-function-template-instances/comment-page-1/#comment-60124</link>
		<dc:creator>arun</dc:creator>
		<pubDate>Wed, 06 May 2009 12:44:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=201#comment-60124</guid>
		<description>Hi Alex,

could you tell me why there is a friend for the &lt;&lt; operator?

&lt;pre&gt;    
friend ostream&amp; operator&lt;&lt; (ostream &amp;out, const Cents &amp;cCents) 
    
{ 
        
out &lt;&lt; cCents.m_nCents &lt;&lt; &quot; cents &quot;; 
        
return out; 
    
} &lt;/pre&gt;

Thanks</description>
		<content:encoded><![CDATA[<p>Hi Alex,</p>
<p>could you tell me why there is a friend for the &lt;&lt; operator?</p>
<pre>
friend ostream&amp; operator&lt;&lt; (ostream &amp;out, const Cents &amp;cCents) 

{ 

out &lt;&lt; cCents.m_nCents &lt;&lt; &quot; cents &quot;; 

return out; 

} </pre>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.learncpp.com/cpp-tutorial/142-function-template-instances/comment-page-1/#comment-59713</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Sat, 02 May 2009 03:08:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=201#comment-59713</guid>
		<description>I answered this question below.  See the response to MarkG&#039;s question.</description>
		<content:encoded><![CDATA[<p>I answered this question below.  See the response to MarkG&#8217;s question.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.learncpp.com/cpp-tutorial/142-function-template-instances/comment-page-1/#comment-59712</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Sat, 02 May 2009 03:03:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=201#comment-59712</guid>
		<description>First off, I changed the example to use a const Cents &amp;cents, which it should have been all along, because operator&lt;&lt; is not modifying the Cents it&#039;s using.

As to why your example didn&#039;t work, let&#039;s start with a simpler one.  Consider the following statement:

&lt;pre&gt;std::cout &lt;&lt; Cents(5) &lt;&lt; std::endl;&lt;!--formatted--&gt;&lt;/pre&gt;

Would you expect this to work if operator&lt;&lt; didn&#039;t take a const Cents reference?

The answer should be no.  Cents(5) is an rvalue, and does not have it&#039;s own address.  Thus, you can&#039;t set a reference to it.

Because Average() is returning a Cents by value, it&#039;s falling into the same trap.  It&#039;s the exact same reason the following doesn&#039;t work:

&lt;pre&gt;
void foo(int &amp;x)
{
}

foo(5); // doesn&#039;t work
&lt;!--formatted--&gt;&lt;/pre&gt;

However, if you make the reference const, then you CAN pass in literals and temporaries.

&lt;pre&gt;
void foo(const int &amp;x)
{
}

foo(5); // works
&lt;!--formatted--&gt;&lt;/pre&gt;

and similarly, the return-by-value Cents from Average works as well.

As to your second question, you can&#039;t return *this from Average because it&#039;s not a member function.</description>
		<content:encoded><![CDATA[<p>First off, I changed the example to use a const Cents &#038;cents, which it should have been all along, because operator&lt;&lt; is not modifying the Cents it&#8217;s using.</p>
<p>As to why your example didn&#8217;t work, let&#8217;s start with a simpler one.  Consider the following statement:</p>
<pre>std::cout &lt;&lt; Cents(5) &lt;&lt; std::endl;<!--formatted--></pre>
<p>Would you expect this to work if operator&lt;&lt; didn&#8217;t take a const Cents reference?</p>
<p>The answer should be no.  Cents(5) is an rvalue, and does not have it&#8217;s own address.  Thus, you can&#8217;t set a reference to it.</p>
<p>Because Average() is returning a Cents by value, it&#8217;s falling into the same trap.  It&#8217;s the exact same reason the following doesn&#8217;t work:</p>
<pre>
void foo(int &amp;x)
{
}

foo(5); // doesn&#039;t work
<!--formatted--></pre>
<p>However, if you make the reference const, then you CAN pass in literals and temporaries.</p>
<pre>
void foo(const int &amp;x)
{
}

foo(5); // works
<!--formatted--></pre>
<p>and similarly, the return-by-value Cents from Average works as well.</p>
<p>As to your second question, you can&#8217;t return *this from Average because it&#8217;s not a member function.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.learncpp.com/cpp-tutorial/142-function-template-instances/comment-page-1/#comment-59711</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Sat, 02 May 2009 02:47:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=201#comment-59711</guid>
		<description>Andreas, I believe the code is correct as written.  We&#039;re not trying to fit the max-template here, but rather we&#039;re overloading the &gt; operator so that the compiler knows how to do c1 &gt; c2 when it encounters that inside the max() template for Cents.

Consider the logical meaning of the following statment:
&lt;pre&gt;
if (c1 &gt; c2)
&lt;!--formatted--&gt;&lt;/pre&gt;

if the &gt; operator returns a bool here, then c1 &gt; c2 returns a bool here and this makes sense.  But if it returns a Cents instead, what does that mean logically?</description>
		<content:encoded><![CDATA[<p>Andreas, I believe the code is correct as written.  We&#8217;re not trying to fit the max-template here, but rather we&#8217;re overloading the &gt; operator so that the compiler knows how to do c1 &gt; c2 when it encounters that inside the max() template for Cents.</p>
<p>Consider the logical meaning of the following statment:</p>
<pre>
if (c1 &gt; c2)
<!--formatted--></pre>
<p>if the &gt; operator returns a bool here, then c1 &gt; c2 returns a bool here and this makes sense.  But if it returns a Cents instead, what does that mean logically?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AndiW</title>
		<link>http://www.learncpp.com/cpp-tutorial/142-function-template-instances/comment-page-1/#comment-57770</link>
		<dc:creator>AndiW</dc:creator>
		<pubDate>Tue, 14 Apr 2009 13:25:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=201#comment-57770</guid>
		<description>Hi Alex,

excellent tutorial! I found a little typo though. The 
Last return statement in the last block of code in the 
&quot;Operators, function calls, and function templates&quot; is 

&lt;pre&gt;return (c1.m_nCents &gt; c2.m_nCents) ? true : false;&lt;!--formatted--&gt;&lt;/pre&gt;

but it should be

&lt;pre&gt;return (c1.m_nCents &gt; c2.m_nCents) ? c1 : c2;&lt;!--formatted--&gt;&lt;/pre&gt;

Also, the return type shouldn&#039;t be bool, but Cents. 
Otherwise it does not fit the max-template. Right? 

Thanks!

Andreas</description>
		<content:encoded><![CDATA[<p>Hi Alex,</p>
<p>excellent tutorial! I found a little typo though. The<br />
Last return statement in the last block of code in the<br />
&#8220;Operators, function calls, and function templates&#8221; is </p>
<pre>return (c1.m_nCents &gt; c2.m_nCents) ? true : false;<!--formatted--></pre>
<p>but it should be</p>
<pre>return (c1.m_nCents &gt; c2.m_nCents) ? c1 : c2;<!--formatted--></pre>
<p>Also, the return type shouldn&#8217;t be bool, but Cents.<br />
Otherwise it does not fit the max-template. Right? </p>
<p>Thanks!</p>
<p>Andreas</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MarkG</title>
		<link>http://www.learncpp.com/cpp-tutorial/142-function-template-instances/comment-page-1/#comment-48817</link>
		<dc:creator>MarkG</dc:creator>
		<pubDate>Fri, 06 Mar 2009 13:38:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=201#comment-48817</guid>
		<description>Hi Alex,

If I use
&lt;pre&gt;
friend std::ostream&amp; operator&lt;&lt; (std::ostream &amp;out, Cents &amp;cCents);
{
out &lt;&lt; cCents.m_nCents &lt;&lt; &quot; cents &quot;;
return out;
}
&lt;/pre&gt;
as my &lt;&lt; overide definition within the Cents class, then the line
&lt;pre&gt;
std::cout &lt;&lt; Average(cArray,4) &lt;&lt; std::endl;
&lt;/pre&gt;
in the main function produces an error in my compiler (see below), which I 
think is related to the chaining of the &lt;&lt; operator.  

error: no match for ‘operator&lt;&lt;’ in ‘std::cout &lt;&lt; Average [with T = Cents](((Cents)(&amp; cArray)), 4)’

If the Average function returned a *this pointer I think this would solve 
the problem (alternatively, I can introduce a dummy Cents variable to 
store the result of Average, and std::cout that).  However, another way to 
resolve this problems is to rewrite the &lt;&lt; override function with a const 
input for Cents, like so:
&lt;pre&gt;
friend std::ostream&amp; operator&lt;&lt; (std::ostream &amp;out, const Cents &amp;cCents);
&lt;/pre&gt;
Now the line 
&lt;pre&gt;
std::cout &lt;&lt; Average(cArray,4) &lt;&lt; std::endl;
&lt;/pre&gt;
compiles without a problem!  Can you help me understand why the addtion of
the const keyword to the input resolves the problem of chaining the output 
of the Average function for Cents?  Also, would it be possible to modify the
template Average function so that for classes it returned a *this pointer
rather than a value?  I suspect not as the function is outside of the Cents
class, but would be interested to know.  This appears to be the same problem
as Ben (post #1) had.
   
Thanks,
  
Mark
  
P.S. Fantastic tutorial by the way!</description>
		<content:encoded><![CDATA[<p>Hi Alex,</p>
<p>If I use</p>
<pre>
friend std::ostream&amp; operator&lt;&lt; (std::ostream &amp;out, Cents &amp;cCents);
{
out &lt;&lt; cCents.m_nCents &lt;&lt; &quot; cents &quot;;
return out;
}
</pre>
<p>as my &lt;&lt; overide definition within the Cents class, then the line</p>
<pre>
std::cout &lt;&lt; Average(cArray,4) &lt;&lt; std::endl;
</pre>
<p>in the main function produces an error in my compiler (see below), which I<br />
think is related to the chaining of the &lt;&lt; operator.  </p>
<p>error: no match for ‘operator&lt;&lt;’ in ‘std::cout &lt;&lt; Average [with T = Cents](((Cents)(&amp; cArray)), 4)’</p>
<p>If the Average function returned a *this pointer I think this would solve<br />
the problem (alternatively, I can introduce a dummy Cents variable to<br />
store the result of Average, and std::cout that).  However, another way to<br />
resolve this problems is to rewrite the &lt;&lt; override function with a const<br />
input for Cents, like so:</p>
<pre>
friend std::ostream&amp; operator&lt;&lt; (std::ostream &amp;out, const Cents &amp;cCents);
</pre>
<p>Now the line </p>
<pre>
std::cout &lt;&lt; Average(cArray,4) &lt;&lt; std::endl;
</pre>
<p>compiles without a problem!  Can you help me understand why the addtion of<br />
the const keyword to the input resolves the problem of chaining the output<br />
of the Average function for Cents?  Also, would it be possible to modify the<br />
template Average function so that for classes it returned a *this pointer<br />
rather than a value?  I suspect not as the function is outside of the Cents<br />
class, but would be interested to know.  This appears to be the same problem<br />
as Ben (post #1) had.</p>
<p>Thanks,</p>
<p>Mark</p>
<p>P.S. Fantastic tutorial by the way!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Grant</title>
		<link>http://www.learncpp.com/cpp-tutorial/142-function-template-instances/comment-page-1/#comment-26374</link>
		<dc:creator>Grant</dc:creator>
		<pubDate>Fri, 05 Sep 2008 08:45:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=201#comment-26374</guid>
		<description>Hi Alex,

Another small typo, just after the introduction of the Cents class the text mentions the &quot;height&quot; class.</description>
		<content:encoded><![CDATA[<p>Hi Alex,</p>
<p>Another small typo, just after the introduction of the Cents class the text mentions the &#8220;height&#8221; class.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

