﻿<?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++博客-xyjzsh-随笔分类-STL学习系列</title><link>http://www.cppblog.com/xyjzsh/category/16050.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 23 Feb 2011 03:22:25 GMT</lastBuildDate><pubDate>Wed, 23 Feb 2011 03:22:25 GMT</pubDate><ttl>60</ttl><item><title>通过函数参数的类型推导模板版本</title><link>http://www.cppblog.com/xyjzsh/archive/2011/02/23/140496.html</link><dc:creator> 呆人</dc:creator><author> 呆人</author><pubDate>Wed, 23 Feb 2011 03:13:00 GMT</pubDate><guid>http://www.cppblog.com/xyjzsh/archive/2011/02/23/140496.html</guid><wfw:comment>http://www.cppblog.com/xyjzsh/comments/140496.html</wfw:comment><comments>http://www.cppblog.com/xyjzsh/archive/2011/02/23/140496.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xyjzsh/comments/commentRss/140496.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xyjzsh/services/trackbacks/140496.html</trackback:ping><description><![CDATA[<p>模板函数如下：<br>template&lt;class T&gt;<br>T max(T x, T y)<br>{<br>&nbsp;&nbsp; return x&gt;y ? x:y;<br>}<br><br>void testMax(void)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;max(1,2);//因为两个参数都是int型，所以编译器会根据参数而推导出使用max&lt;int&gt;这个版本<br>&nbsp;&nbsp;&nbsp;&nbsp;max(1.2,3.4);//通过两个参数都为double型，推导出使用max&lt;double&gt;这个版本<br>&nbsp;&nbsp;&nbsp; max('a',12);//会出现编译错误，因为两个参数，一个为char ,另一个为int ,所以编译器无法根据类型推导出使用哪一个版本<br>&nbsp;&nbsp;&nbsp; //可以通过显示的制定使用哪个版本，从而解决这个问题<br>&nbsp;&nbsp;&nbsp; max&lt;int&gt;('a',12);<br>}</p>
<img src ="http://www.cppblog.com/xyjzsh/aggbug/140496.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xyjzsh/" target="_blank"> 呆人</a> 2011-02-23 11:13 <a href="http://www.cppblog.com/xyjzsh/archive/2011/02/23/140496.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>