前言

boost::function和boost:bind是一对强大的利器。相信用过的童鞋多少有些体会。

虽然平时在用boost::function,但是用的时候心中总会一些不安,因为不知道它是怎么实现的。于是,就自己琢磨着简单的实现一下,搞明白基本的原理。

对于这个简单实现,有以下几个目标:

  • 选取比较常见的接收2个参数的情况。
  • 支持普通函数/函数指针、成员函数指针。
  • 兼容函数对象、函数适配器/boost::bind。

实现

首先,定义一个基类:

template<typename R, typename T1, typename T2>
class base
{
public:
virtual ~base()
{
} virtual R operator()(T1, T2) = 0;
};

然后再实现一个普通函数/函数指针的版本:

template<typename R, typename T1, typename T2>
class func : public base<R, T1, T2>
{
public:
func(R (*ptr)(T1, T2))
: ptr_(ptr)
{
} virtual R operator()(T1 a, T2 b)
{
return ptr_(a, b);
} private:
R (*ptr_)(T1, T2);
};

接着,实现支持成员函数指针的版本:

template<typename R, typename Class, typename T>
class member : public base<R, Class, T>
{
}; template<typename R, typename Class, typename T>
class member<R, Class*, T> : public base<R, Class*, T>
{
public:
member(R (Class::*ptr)(T))
: ptr_(ptr)
{
} virtual R operator()(Class* obj, T a)
{
return (obj->*ptr_)(a);
} private:
R (Class::*ptr_)(T);
};

可能有的童鞋要问,为什么这里要有一个空的member类呢?这个问题放到下面解释。

自然的轮到最后一个种情况,函数对象/boost::bind类型的了:

template<typename T, typename R, typename T1, typename T2>
class functor : public base<R, T1, T2>
{
public:
functor(const T& obj)
: obj_(obj)
{
} virtual R operator()(T1 a, T2 b)
{
return obj_(a, b);
} private:
T obj_;
};

最后,就是可用的function类了,实现如下:

template<typename T>
class function
{
}; template<typename R, typename T1, typename T2>
class function<R (T1, T2)>
{
public:
template<typename Class, typename _R, typename _T2>
function(_R (Class::*ptr)(_T2))
: ptr_(new member<R, T1, T2>(ptr))
{
} template<typename _R, typename _T1, typename _T2>
function(_R (*ptr)(_T1, _T2))
: ptr_(new func<R, T1, T2>(ptr))
{
} template<typename T>
function(const T& obj)
: ptr_(new functor<T, R, T1, T2>(obj))
{
} ~function()
{
delete ptr_;
} virtual R operator()(T1 a, T2 b)
{
return ptr_->operator()(a, b);
} private:
base<R, T1, T2>* ptr_;
};

大家可能注意到了,和前面的member类一样,function也有一个空的类,那么这些有什么用呢?

这么做的原因,主要是利用模板偏特化来进行类型萃取,正常的function声明的时候,比如function<int (int, int)>而不是func<int, int, int>。所以用模板的偏特化的版本

template<typename R, typename T1, typename T2>
class function<R (T1, T2)>

就可以把int (int, int)萃取为R = int,T1 = int,T2 = int了。

同理,对于member类,由于一般我们将成员函数指针绑定到function的时候,比如int function(Type*, int),其中Type是成员函数所属类。也就是说在function中的成员ptr_的类型是base<int, Type*, int>,那么在function的构造函数中构造的member类的类型就是member<int, Type*, int>,也就是Class = Type*,但是我们需要的却是Class = Type。所以这里得用偏特化萃取一下:

template<typename R, typename Class, typename T>
class member<R, Class*, T> : public base<R, Class*, T>

这样得到的Class模板形参就会被编译器决议为Type,而不是Type*了。

另外提一下,在function的3种情况的构造函数是模板成员函数,而不是普通成员函数:

    template<typename Class, typename _R, typename _T2>
function(_R (Class::*ptr)(_T2))
: ptr_(new member<R, T1, T2>(ptr))
{
} template<typename _R, typename _T1, typename _T2>
function(_R (*ptr)(_T1, _T2))
: ptr_(new func<R, T1, T2>(ptr))
{
} template<typename T>
function(const T& obj)
: ptr_(new functor<T, R, T1, T2>(obj))
{
}

前2种情况,普通函数/函数指针对应的构造函数和成员函数指针对应的构造函数实现为成员模板,主要是为了兼容参数的隐式转换,例如声明一个function的类型为function<int (int, int)> foo,调用的时候却传入两个double类型,foo(1.1, 2.2), double类型隐式转换成了int类型。这样也符合boost:function本来的兼容可转换的调用物这一特性。

而第3种情况的成员模板,是为了获取传入的函数对象/boost::bind的类型,以便在存储在functor的数据成员中,这也是为什么functor类的模板参数比其他版本多了一个的原因。

然后,我们来测试一下:

int get(int a, int b)
{
std::cout << a+b << std::endl;
return 0;
} class Point
{
public:
int get(int a)
{
std::cout << "Point::get called: a = "<< a << std::endl;
return a;
}
int doit(int a, int b)
{
std::cout << "Point::doit called: a = "<< a+b << std::endl;
return a+b;
}
}; int main(int argc, char const *argv[])
{
function<int (int, int)> foo(get);
foo(10.1, 10.3); function<int (Point*, int)> bar(&Point::get);
Point point;
bar(&point, 30); function<int (int, int)> obj(boost::bind(&Point::doit, &point, _1, _2));
obj(90, 100);
}

结果为:

20
Point::get called: a = 30
Point::doit called: a = 190

可以看到,输出的内容正是所期望的结果。

参考文献

  1. boost中文手册. Improved Function Object Adapters 改良的函数对象适配器

(完)

boost::function的简单实现的更多相关文章

  1. boost::bind的简单实现

    前言 在上一篇blog中简单的实现了boost::function,支持带有2个参数的函数/函数指针,函数对象,函数适配器/bind类,以及带有1个参数的成员函数指针. 本文接着来介绍如何实现一个简单 ...

  2. boost::bind 和 boost::function 基本用法

    这是一篇介绍bind和function用法的文章,起因是近来读陈硕的文章,提到用bind和function替代继承,于是就熟悉了下bind和function的用法,都是一些网上都有的知识,记录一下,期 ...

  3. [转] boost::function用法详解

    http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost/fun ...

  4. [置顶] 编程模仿boost::function和boost::bind

    boost::function和boost::bind结合使用是非常强大的,他可以将成员函数和非成员函数绑定对一个对象上,实现了类似C#的委托机制.委托在许多时候可以替代C++里面的继承,实现对象解耦 ...

  5. boost function对象

    本文根据boost的教程整理. 主要介绍boost function对象的用法. boost function boost function是什么 boost function是一组类和模板组合,用于 ...

  6. boost::function用法详解

    要开始使用 Boost.Function, 就要包含头文件 "boost/function.hpp", 或者某个带数字的版本,从 "boost/function/func ...

  7. boost::function的用法

    本片文章主要介绍boost::function的用法. boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象. 1.  介绍 Boost.Func ...

  8. 以boost::function和boost:bind取代虚函数

    转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是“继承就像一条贼船,上去就下不 ...

  9. boost::function实践——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》

    代码段1: #include <boost/function.hpp> #include <iostream> float mul_ints(int x, int y) { r ...

随机推荐

  1. C++ 学习笔记之——文件操作和文件流

    1. 文件的概念 对于用户来说,常用到的文件有两大类:程序文件和数据文件.而根据文件中数据的组织方式,则可以将文件分为 ASCII 文件和二进制文件. ASCII 文件,又称字符文件或者文本文件,它的 ...

  2. 接触到的一些数据结构: LIST_ENTRY, TAILQ

    双链表: LIST_ENTRY: typedef struct _LIST_ENTRY { struct _LIST_ENTRY  *Flink; follow: next entry, header ...

  3. linux mysql 链接数太小

    Data source rejected establishment of connection,  message from server: "Too many connections&q ...

  4. Struts1防止表单重复提交

    package org.zln.struts.action; import org.apache.struts.action.Action; import org.apache.struts.acti ...

  5. 2018牛客多校第一场 B.Symmetric Matrix

    题意: 构造一个n*n的矩阵,使得Ai,i = 0,Ai,j = Aj,i,Ai,1+Ai,2+...+Ai,n = 2.求种类数. 题解: 把构造的矩阵当成邻接矩阵考虑. 那么所有点的度数都为2,且 ...

  6. 安徽师大附中%你赛day9 T2 富 解题报告

    富 题目背景 出于某些原因, 苟先生在追杀富先生. 题目描述 富先生所在的地方是一个\(n\times m\)的网格,苟先生排出了他的狼狗大军,共有\(k\)条狗,第\(i\)条狗所在的位置为\((x ...

  7. ionic2-键盘覆盖输入框和返回键问题解决方案

    http://blog.csdn.net/u012979009/article/details/52514892有遇到这个问题的去这个地址看

  8. POJ3660:Cow Contest(Floyd传递闭包)

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16941   Accepted: 9447 题目链接 ...

  9. remove computer from join with powershell

    Removes the local computer from its domain. Remove-Computer [-UnjoinDomainCredential] <PSCredenti ...

  10. (转)Django常用命令

    转自GoodSpeed,http://www.cnblogs.com/cacique/archive/2012/09/30/2709145.html . . . . .