支持返回类型
目前的function_object_ref版本只能支持void返回类型。我希望能够让它支持多种返回类型,最简单的方法是添加一个模板参数。请看下面的代码:
template<typename function_object_type,typename element_type,typename return_type>
class function_object_ref{
public:
explicit function_object_ref(function_object_type & object):object_(&object){
}
return_type operator()(element_type e){
return object_->operator()(e);
}
private:
function_object_type* object_;
};
class compare{
public:
compare(int x):x_(x){
}
bool operator()(int x){
return x_==x;
}
int x() const{
return x_;
}
private:
int x_;
};
int main(int argc, char** argv) {
compare c(2);
function_object_ref<compare,int,bool> wrapper_c(c);
vector<int>::iterator itor = std::find_if(v.begin(),v.end(),wrapper_c);
cout<<*itor;
return 0;
}
boost::result_of
上面我的方法虽然解决了问题,但是又引入了一个模板参数。能不能自动推导出返回类型,少一个模板参数呢?
即将到来的C++11引入了decltype用于编译时推导返回类型。不过目前在我的gcc工程中还没有使用C++11,我引入boost::result_of来推导返回类型。但是result_of却要求function object要定义返回类型result_type.所以减少一个模板参数的同时,又在函数对象中增加了一个typedef,示例如下:
#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/utility/result_of.hpp>
using namespace std;
template<typename FunctionObjectType, typename ElementType>
class FunctionObjectRef {
public:
explicit FunctionObjectRef(FunctionObjectType & object): object_(&object) {
}
typedef typename boost::result_of<FunctionObjectType(ElementType)>::type ReturnType;
ReturnType operator()(ElementType e) {
return object_->operator()(e);
}
private:
FunctionObjectType* object_;
};
class Compare {
public:
typedef bool result_type;
Compare(int x):x_(x){
}
bool operator()(int x){
return x_==x;
}
int x() const{
return x_;
}
private:
int x_;
};
int main(int argc, char** argv) {
vector<int> v;
v.push_back(2);
v.push_back(1);
Compare c(2);
FunctionObjectRef<Compare, int> wrapper_c(c);
vector<int>::iterator itor = std::find_if(v.begin(), v.end(), wrapper_c);
cout << *itor;
return 0;
}
感兴趣的可以参考boost文档:http://www.boost.org/doc/libs/1_47_0/libs/utility/utility.htm#result_of
result_of实现非常简单,就是什么也不做的模板类:
template<typename F> struct result_of;
当编译器编译这行代码的时候,就能自动推断出ReturnType
FunctionObjectRef<Compare, int> wrapper_c(c);
typedef typename boost::result_of<FunctionObjectType(ElementType)>::type ReturnType;
分享到:
相关推荐
这一段除了在学习 matlab oop 还把 matlab并行计算的研究学习了一下。 现在想实际做点东西。呵呵~ 在网上一顿搜索,在科学网找到了一个不错的教程(当然matlab oop 的 help文件也是很好的教程) 所以分享一下 ...
Object.prototype.a = function(){}; Function.prototype.b = function(){}; var f = new F(); 问:f 能取到a,b吗?原理是什么? 乍一看真的有点懵,仔细研究了一下,发现还是对原型理解不透彻,所以总结一篇,填个...
To achieve accurate visual object tracking and overcome the difficulties brought by the object deformation, occlusion, and illumination variations, a particle filter for object tracking algorithm ...
目录 1、时间格式化常规思路2、时间格式化toLocaleString() 1、时间格式化常规思路 2、时间格式化toLocaleString() 研究Object对象的时候,看到了 toLocaleString() 这个方法可以很简单的实现时间格式化。...
一、临界区 ...procedure TForm1.Button3Click(Sender: TObject); begin Lock:=3; // 互斥 hMutex:=CreateMutex(0,False,nil); TMyThread.Create(False); TMyThread.Create(False); end; end.
Function formPrint(ByVal obj As Object, _ ByVal font As String, _ ByVal fontsize As Double, _ ByVal x As Double, _ ByVal y As Double, _ ByVal str As String) As Boolean '功能:实现在窗体打印,并...
system 目 录 摘要 I Abstract II 1 引言 1 1.1 选题的依据和意义 1 1.2 课题研究目的 1 2 系统分析 2 2.1 系统需求分析 2 2.2 可行性分析 3 2.2.1技术的可行性 3 2.2.2经济的可行性 3 2.2.3操作可行性 3 2.2.4法律...
sparsity of text object, the high complexity of algorithm and the requirement of prior knowledge and so on, have seriously hampered the development of TM. In the final analysis, these problems in TM ...
Return True End Function Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click If Me.ViewState("flag") = True Then Page....
当然我们可以研究js库的源码, 也可以自己去发明轮子试试看, 其过程还是挺有趣的…下面我就来实现下页面元素的拖拽功能 现在就开始着手实现, 让我们从最顶层的方法讲起, 它用于初始化一个drag object, 方法的声明如下...
我最近研究了一下,并分享给大家,希望能对大家有所帮助。 JavaScript中检测对象的方法 1.typeof操作符 这种方法对于一些常用的类型来说那算是毫无压力,比如Function、String、Number、Undefined等,但是要是检测...
The styles are grouped into nine categories: historical, basic, function composition, objects and object interactions, reflection and metaprogramming, adversity, data-centric, concurrency, and ...
◆Delphi多线程编程之三 同步读写全局数据 ◆(乌龙哈里2008-10-12) 收藏 ◆Delphi多线程编程之三同步读写全局数据 ◆(乌龙哈里2008-10-12) (调试环境:Delphi2007+WinXPsp3 例程:Tst_Thread3.dpr) 开始...
The styles are grouped into nine categories: historical, basic, function composition, objects and object interactions, reflection and metaprogramming, adversity, data-centric, concurrency, and ...
最近研究个PB写的软件,运行时以下错误,发现缺少POWERPRN.dll,找了...错误代码“14”,错误信息:Error opining DLL powerprn.dll for external function at line 19 in constructor event of object n_powerprinter
迭代是对对象的深入研究。 如果传递了iteratee,则将它绑定到上下文对象。 每次调用iteratee时都会调用三个参数:(element,path,obj)。 Example: let obj = { name : "test" , flags : { checked : true ...
Function Handling Functions XLIII. Gettext XLIV. GMP Functions XLV. GNU Readline XLVI. GNU Recode Functions XLVII. HTTP 函数 XLVIII. Hyperwave API Functions XLIX. Hyperwave Functions L. IBM ...
W3C组织正在研究一种名为RDF(Resource Description Framework)的metadata处理方法,可以自动交换信息,W3C宣称,使用RDF配合数字签名,将使网络中存在"真实可信"的电子商务。 六.显示 单独用XMl不能显示页面,...
当我我在后台执行ajax操作的时候,success,error不能成功的返回提示的信息,而是返回object object这样的一条错误提示。 研究了很久发现是版本的问题,新改版的ThinkPHP对DWZ中的Ajax技术中的一点不符合版本的问题 所以...