---
title: "template学习：一个Functor的使用例子"
author: "ah_fu"
date: "2008-01-12T18:52:00Z"
tags: ["C", "C#", "C++"]
slug: "template-functor"
draft: false
lastmod: "2008-01-12T18:52:00Z"
id: 281571
url: "https://ah-fu.iteye.com/blog/281571"
---

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#include &lt;stdio.h&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)// 模板，可以调用函数或者Functor  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)template &lt;typename Functor&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)void TestTemplate(Functor func)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    func("TestTemplate");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)// C函数风格  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)void TestFunction(const char* str)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    printf("TestFunction:%s ", str);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)// Functor风格  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)struct TestFunctor  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    void operator()(const char* str)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        printf("TestFunctor:%s ", str);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    TestFunctor()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        printf("TestFunctor() ");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    ~TestFunctor()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        printf("~TestFunctor() ");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)};  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)int main()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    TestTemplate(TestFunction);  //函数指针作为调用参数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    printf("========================================== ");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    TestTemplate( TestFunctor() );  //Functor临时对象作为调用参数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    printf("========================================== ");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    TestFunctor obj;  //生成一个Functor的实例  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    TestTemplate&lt;TestFunctor&&gt;(obj);  //使用引用类型来调用，避免拷贝  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return 1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)