<?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>评论：C语言中实现模板函数小结</title>
	<atom:link href="http://www.boluor.com/summary-about-template-function-under-c.html/feed" rel="self" type="application/rss+xml" />
	<link>http://www.boluor.com/summary-about-template-function-under-c.html</link>
	<description></description>
	<lastBuildDate>Wed, 01 Sep 2010 12:01:11 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>由：voxdei</title>
		<link>http://www.boluor.com/summary-about-template-function-under-c.html/comment-page-1#comment-1177</link>
		<dc:creator>voxdei</dc:creator>
		<pubDate>Sat, 26 Jun 2010 17:40:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.boluor.com/?p=81#comment-1177</guid>
		<description>#include
#include
#include

#ifndef TYPE
#   define TYPE int
#endif

TYPE cmp(TYPE *a, TYPE *b)
{
    return (*a - *b);
}

int main()
{
    TYPE x = 1;
    TYPE y = -1;
    (void)printf(&quot;%d\n&quot;, cmp(&amp;x, &amp;y));
    return 0;
}

gcc evil.c -DTYPE=char -std=c99
./a.out
2

gcc evil.c -DTYPE=bool -std=c99
./a.out
0

不过很邪恶啊</description>
		<content:encoded><![CDATA[<p>#include<br />
#include<br />
#include</p>
<p>#ifndef TYPE<br />
#   define TYPE int<br />
#endif</p>
<p>TYPE cmp(TYPE *a, TYPE *b)<br />
{<br />
    return (*a &#8211; *b);<br />
}</p>
<p>int main()<br />
{<br />
    TYPE x = 1;<br />
    TYPE y = -1;<br />
    (void)printf(&#8220;%d\n&#8221;, cmp(&amp;x, &amp;y));<br />
    return 0;<br />
}</p>
<p>gcc evil.c -DTYPE=char -std=c99<br />
./a.out<br />
2</p>
<p>gcc evil.c -DTYPE=bool -std=c99<br />
./a.out<br />
0</p>
<p>不过很邪恶啊</p>
]]></content:encoded>
	</item>
	<item>
		<title>由：boluor</title>
		<link>http://www.boluor.com/summary-about-template-function-under-c.html/comment-page-1#comment-598</link>
		<dc:creator>boluor</dc:creator>
		<pubDate>Thu, 10 Dec 2009 05:30:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.boluor.com/?p=81#comment-598</guid>
		<description>Felix021当时写的。Sandy写的上面已经有了。主要是代码风格，他们两个的很清晰。
&lt;pre lang=&quot;c&quot;&gt;
#include &lt;stdio.h&gt;

int lessthan(void *a, void *b){
    int *aa = (int *)a, *bb = (int *)b;
    return (*aa &lt; *bb) ? 1 : 0;
}

int findmin(void *a, int a_size, int a_num,
            int (*lessthan)(void *, void*))
{
    char *p = a, *q;
    int i;
    if(a_num &lt;= 0) return -1;
    for (i = 1; i &lt; a_num; ++i){
        q = (char*)a + i * a_size;
        if(lessthan(p, q) == 0){
            p = q;
        }
    }
    return (p - (char *)a) / a_size;
}

int main(){
    int d[] = {5,3,6,1,2,4,7,8,9};
    int index = findmin(d, sizeof(int), 9, lessthan);
    printf(&quot;d[%d] = %d\n&quot;, index, d[index]);
    return 0;
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Felix021当时写的。Sandy写的上面已经有了。主要是代码风格，他们两个的很清晰。</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:Courier new,verdana;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> lessthan<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span>a<span style="color: #339933;">,</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>b<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> <span style="color: #339933;">*</span>aa <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>a<span style="color: #339933;">,</span> <span style="color: #339933;">*</span>bb <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>b<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>aa <span style="color: #339933;">&lt;</span> <span style="color: #339933;">*</span>bb<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #0000dd;">1</span> <span style="color: #339933;">:</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> findmin<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span>a<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> a_size<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> a_num<span style="color: #339933;">,</span>
            <span style="color: #993333;">int</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>lessthan<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*,</span> <span style="color: #993333;">void</span><span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>p <span style="color: #339933;">=</span> a<span style="color: #339933;">,</span> <span style="color: #339933;">*</span>q<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>a_num <span style="color: #339933;">&lt;=</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> a_num<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        q <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span><span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>a <span style="color: #339933;">+</span> i <span style="color: #339933;">*</span> a_size<span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>lessthan<span style="color: #009900;">&#40;</span>p<span style="color: #339933;">,</span> q<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            p <span style="color: #339933;">=</span> q<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>p <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>a<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> a_size<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> d<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #0000dd;">5</span><span style="color: #339933;">,</span><span style="color: #0000dd;">3</span><span style="color: #339933;">,</span><span style="color: #0000dd;">6</span><span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span><span style="color: #0000dd;">2</span><span style="color: #339933;">,</span><span style="color: #0000dd;">4</span><span style="color: #339933;">,</span><span style="color: #0000dd;">7</span><span style="color: #339933;">,</span><span style="color: #0000dd;">8</span><span style="color: #339933;">,</span><span style="color: #0000dd;">9</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> index <span style="color: #339933;">=</span> findmin<span style="color: #009900;">&#40;</span>d<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">9</span><span style="color: #339933;">,</span> lessthan<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;d[%d] = %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> index<span style="color: #339933;">,</span> d<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>由：agassi_p</title>
		<link>http://www.boluor.com/summary-about-template-function-under-c.html/comment-page-1#comment-596</link>
		<dc:creator>agassi_p</dc:creator>
		<pubDate>Thu, 10 Dec 2009 05:02:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.boluor.com/?p=81#comment-596</guid>
		<description>博主，你好！能不能把--他和Sandy写出来他们写的FindMin--他们写的代码贴出来看看呀，谢谢了！！
“Felix021说我的代码风格不好，开始不明白，不过等他和Sandy写出来他们写的FindMin后，就明白了，我的代码太晦涩了，还是写个自己看的，别人看了看不出大概，真的多注意了。
”</description>
		<content:encoded><![CDATA[<p>博主，你好！能不能把&#8211;他和Sandy写出来他们写的FindMin&#8211;他们写的代码贴出来看看呀，谢谢了！！<br />
“Felix021说我的代码风格不好，开始不明白，不过等他和Sandy写出来他们写的FindMin后，就明白了，我的代码太晦涩了，还是写个自己看的，别人看了看不出大概，真的多注意了。<br />
”</p>
]]></content:encoded>
	</item>
	<item>
		<title>由：Felix021</title>
		<link>http://www.boluor.com/summary-about-template-function-under-c.html/comment-page-1#comment-24</link>
		<dc:creator>Felix021</dc:creator>
		<pubDate>Wed, 27 May 2009 04:51:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.boluor.com/?p=81#comment-24</guid>
		<description>差不多吧，我以前写的很多东西也很挫。
不过C++的模板，那个typename的实现差不多也就是typedef吧。
那个调试方法我也用过，确实很挫，把代码搞的一团糟，太难看了。
特别是第二段代码17行开始的#if，那简直就是搞笑。
最后，我倾向于直接返回索引，而不是把保存索引的int的地址传过去，太晦涩了。</description>
		<content:encoded><![CDATA[<p>差不多吧，我以前写的很多东西也很挫。<br />
不过C++的模板，那个typename的实现差不多也就是typedef吧。<br />
那个调试方法我也用过，确实很挫，把代码搞的一团糟，太难看了。<br />
特别是第二段代码17行开始的#if，那简直就是搞笑。<br />
最后，我倾向于直接返回索引，而不是把保存索引的int的地址传过去，太晦涩了。</p>
]]></content:encoded>
	</item>
	<item>
		<title>由：sandy</title>
		<link>http://www.boluor.com/summary-about-template-function-under-c.html/comment-page-1#comment-21</link>
		<dc:creator>sandy</dc:creator>
		<pubDate>Tue, 26 May 2009 09:58:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.boluor.com/?p=81#comment-21</guid>
		<description>不是我说你，这文章写的实在是令人无语……
要是让felix看到了估计你又得被BS了。

首先你知道何为“模板”？何为模板函数？你写的三个例子一个模板都没有————别狡辩说C里面没有模板，就算用define来模拟模板也完全不是你那个样子，你根本把模板的概念就没搞清楚。


其次，你的方法2，你在哪里看到说“这种方法比较好，方便调试”————把说这话的人找出来，我非找他论理不可，简直是不堪入目的写法。

方法3，不要以为csdn上的人就牛B了，那个方法是更加的糟糕，不仅没有解决问题，反倒增加了复杂度降低了效率，不知道你怎么还认为这算一种好方法……你可以让felix来看看，看他怎么说……</description>
		<content:encoded><![CDATA[<p>不是我说你，这文章写的实在是令人无语……<br />
要是让felix看到了估计你又得被BS了。</p>
<p>首先你知道何为“模板”？何为模板函数？你写的三个例子一个模板都没有————别狡辩说C里面没有模板，就算用define来模拟模板也完全不是你那个样子，你根本把模板的概念就没搞清楚。</p>
<p>其次，你的方法2，你在哪里看到说“这种方法比较好，方便调试”————把说这话的人找出来，我非找他论理不可，简直是不堪入目的写法。</p>
<p>方法3，不要以为csdn上的人就牛B了，那个方法是更加的糟糕，不仅没有解决问题，反倒增加了复杂度降低了效率，不知道你怎么还认为这算一种好方法……你可以让felix来看看，看他怎么说……</p>
]]></content:encoded>
	</item>
	<item>
		<title>由：boluor</title>
		<link>http://www.boluor.com/summary-about-template-function-under-c.html/comment-page-1#comment-17</link>
		<dc:creator>boluor</dc:creator>
		<pubDate>Wed, 20 May 2009 08:13:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.boluor.com/?p=81#comment-17</guid>
		<description>大家的劳动成果阿.呵呵.</description>
		<content:encoded><![CDATA[<p>大家的劳动成果阿.呵呵.</p>
]]></content:encoded>
	</item>
	<item>
		<title>由：李博 CSDN hikaliv</title>
		<link>http://www.boluor.com/summary-about-template-function-under-c.html/comment-page-1#comment-16</link>
		<dc:creator>李博 CSDN hikaliv</dc:creator>
		<pubDate>Tue, 19 May 2009 18:37:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.boluor.com/?p=81#comment-16</guid>
		<description>我来了。写得不错。
分析得很深刻啊。</description>
		<content:encoded><![CDATA[<p>我来了。写得不错。<br />
分析得很深刻啊。</p>
]]></content:encoded>
	</item>
</channel>
</rss>
