﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Edgard</title><link>http://www.cppblog.com/Edgard/</link><description /><language>zh-cn</language><lastBuildDate>Mon, 13 Apr 2026 09:37:45 GMT</lastBuildDate><pubDate>Mon, 13 Apr 2026 09:37:45 GMT</pubDate><ttl>60</ttl><item><title>讨论FunctionTemplate申明的隐藏性（Visibility）</title><link>http://www.cppblog.com/Edgard/archive/2005/12/15/1805.html</link><dc:creator>Edgard</dc:creator><author>Edgard</author><pubDate>Thu, 15 Dec 2005 13:10:00 GMT</pubDate><guid>http://www.cppblog.com/Edgard/archive/2005/12/15/1805.html</guid><wfw:comment>http://www.cppblog.com/Edgard/comments/1805.html</wfw:comment><comments>http://www.cppblog.com/Edgard/archive/2005/12/15/1805.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cppblog.com/Edgard/comments/commentRss/1805.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Edgard/services/trackbacks/1805.html</trackback:ping><description><![CDATA[<P>看看两端代码，区别重要在：<BR><FONT color=#000000><STRONG>一个是：inline int const&amp; max申明在template &lt;typename T&gt; <BR>inline T const&amp; max之前。</STRONG></FONT><BR><BR>// maximum of two int values <BR>inline int const&amp; max (int const&amp; a, int const&amp; b) <BR>{ <BR>&nbsp;&nbsp;&nbsp; return a&lt;b?b:a; <BR>}</P>
<P>// maximum of two values of any type <BR>template &lt;typename T&gt; <BR>inline T const&amp; max (T const&amp; a, T const&amp; b) <BR>{ <BR>&nbsp;&nbsp;&nbsp; return a&lt;b?b:a; <BR>} </P>
<P>// maximum of three values of any type <BR>template &lt;typename T&gt; <BR>inline T const&amp; max (T const&amp; a, T const&amp; b, T const&amp; c) <BR>{ <BR>&nbsp;&nbsp;&nbsp; return max (max(a,b), c);&nbsp;<BR>}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR><BR><STRONG>一个是：inline int const&amp; max申明在template &lt;typename T&gt; <BR>inline T const&amp; max之后。<BR></STRONG><BR>// maximum of two values of any type <BR>template &lt;typename T&gt; <BR>inline T const&amp; max (T const&amp; a, T const&amp; b) <BR>{ <BR>&nbsp;&nbsp;&nbsp; return a&lt;b?b:a; <BR>} </P>
<P>// maximum of three values of any type <BR>template &lt;typename T&gt; <BR>inline T const&amp; max (T const&amp; a, T const&amp; b, T const&amp; c) <BR>{ <BR>&nbsp;&nbsp;&nbsp; return max (max(a,b), c);&nbsp;<BR>}<BR><BR>// maximum of two int values <BR>inline int const&amp; max (int const&amp; a, int const&amp; b) <BR>{ <BR>&nbsp;&nbsp;&nbsp; return a&lt;b?b:a; <BR>} <BR><BR><STRONG>调用程序：<BR></STRONG>int&nbsp;main( )<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//当然这里本来就写得不好，要先显式申明写局部变量......<BR><STRONG>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 看你了解多少，讨论讨论两种执行可能的执行路径，即：FunctionTemplate的调用路径！！！</STRONG><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;::max( 4, 10 ,15 ); <STRONG><BR></STRONG><BR>}&nbsp;<BR></P><img src ="http://www.cppblog.com/Edgard/aggbug/1805.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Edgard/" target="_blank">Edgard</a> 2005-12-15 21:10 <a href="http://www.cppblog.com/Edgard/archive/2005/12/15/1805.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>VC编译器对FunctionTemplate实例化的优化</title><link>http://www.cppblog.com/Edgard/archive/2005/12/15/1804.html</link><dc:creator>Edgard</dc:creator><author>Edgard</author><pubDate>Thu, 15 Dec 2005 12:43:00 GMT</pubDate><guid>http://www.cppblog.com/Edgard/archive/2005/12/15/1804.html</guid><wfw:comment>http://www.cppblog.com/Edgard/comments/1804.html</wfw:comment><comments>http://www.cppblog.com/Edgard/archive/2005/12/15/1804.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/Edgard/comments/commentRss/1804.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Edgard/services/trackbacks/1804.html</trackback:ping><description><![CDATA[<P>VC编译器对FunctionTemplate实例化的优化：<BR>看下面Template代码：<BR><BR>inline int const&amp; max (int const&amp; a, int const&amp; b) <BR>{ <BR>&nbsp;&nbsp;&nbsp; return a&lt;b?b:a; <BR>} </P>
<P>// maximum of two values of any type <BR>template &lt;typename T&gt; <BR>inline T const&amp; max (T const&amp; a, T const&amp; b) <BR>{ <BR>&nbsp;&nbsp;&nbsp; return a&lt;b?b:a; <BR>} </P>
<P>// maximum of three values of any type <BR>template &lt;typename T&gt; <BR>inline T const&amp; max (T const&amp; a, T const&amp; b, T const&amp; c) <BR>{ <BR>&nbsp;&nbsp;&nbsp; return max (max(a,b), c); <BR>} <BR><BR>inline int const&amp; max (int const&amp; a, int const&amp; b) 已经是template &lt;typename T&gt; <BR>inline T const&amp; max (T const&amp; a, T const&amp; b) 对类型int的实例化，所以，凡是需要调用max&lt;int&gt;时，VC&nbsp;编译器都不在再用max&lt;int&gt;来Instantiate max Function Template.<BR><BR><BR>int main () <BR>{&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;::max(7, 42, 68);&nbsp;// calls the template for three arguments first, then call inline int const&amp; max &nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</P>
<P>&nbsp;&nbsp;&nbsp; ::max(7.0, 42.0);&nbsp;// calls max&lt;double&gt; (by argument deduction) <BR>&nbsp;&nbsp;&nbsp; ::max('a', 'b');&nbsp;// calls max&lt;char&gt; (by argument deduction) <BR>&nbsp;&nbsp;&nbsp; ::max(7, 42);&nbsp;&nbsp;// calls the nontemplate for two ints&nbsp;<BR>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;::max&lt;&gt;(7, 42);&nbsp;&nbsp;// call inline int const&amp; max<BR>&nbsp;&nbsp;&nbsp;&nbsp;::max&lt;int&gt;(7, 42);&nbsp;// call inline int const&amp; max<BR>&nbsp;&nbsp;&nbsp;&nbsp;::max&lt;int&gt;(7.0, 42.0);&nbsp;// call inline int const&amp; max<BR>&nbsp;&nbsp;&nbsp; ::max&lt;double&gt;(7, 42); // calls max&lt;double&gt; (no argument deduction) </P>
<P>&nbsp;&nbsp;&nbsp;return 0;<BR>} <BR></P><img src ="http://www.cppblog.com/Edgard/aggbug/1804.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Edgard/" target="_blank">Edgard</a> 2005-12-15 20:43 <a href="http://www.cppblog.com/Edgard/archive/2005/12/15/1804.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>