function源码分析 前言

写给我自己看的

在接触c++function前,我先接触了C#的委托,

    class Test
    {
        delegate int delegate_test(int a,int b);
        delegate_test TestDelegate;
        public void Run()
        {
            TestDelegate=this.Add;
            TestDelegate(1, 2);
        }
        int Add(int a,int b)
        {
            return a+b;
        }
    }
使用起来很是方便,当时想到C++也可以实现一个类似的,因为template可以完成,当时完成的版本大致如下:

#include <assert.h>
//基类定义接口Invoke,由子类实现
template<typename Return, typename Para>
class Function1Base
{
public:
	virtual Return Invoke(Para para) = 0;
};
// 对函数指针包装
template<typename Return, typename Para>
class Function1Fun :public Function1Base<Return, Para>
{
public:
	typedef Return(*FunType)(Para);
	Function1Fun(FunType fun)
	{
		_fun = fun;
	}
private:
	virtual Return Invoke(Para para)
	{
		return (*_fun)(para);
	}
	FunType  _fun;
};

// 对成员函数指针包装
template<typename Type, typename Return, typename Para>
class Function1MemFun :public Function1Base<Return, Para>
{
public:
	typedef Return(Type::*FunType)(Para);
	Function1MemFun(FunType fun, Type* pType)
	{
		_fun = fun;
		_pType = pType;
	}
private:
	virtual Return Invoke(Para para)
	{
		return (_pType->*_fun)(para);
	}
	FunType _fun;
	Type* _pType;
};
// 一个参数的特化版本
template<typename Return, typename Para>
class function1
{
public:
	typedef Function1Base<Return, Para> BaseType;
	function1(BaseType* pBase)
	{
		_pBase = pBase;
	}
	~function1()
	{
		if (_pBase) {
			delete _pBase;
			_pBase = NULL;
		}
	}
	Return operator()(Para para)
	{
		return _pBase->Invoke(para);//调用
	}
private:
	function1(const function1&);
	function1 operator=(const function1&);
	BaseType* _pBase;
};
template<typename Type,typename Return, typename Para>
inline Function1Base<Return, Para>* bind(Return(Type::*fun)(Para), Type* pType)
{
	return new Function1MemFun<Type,Return,Para>(fun, pType);
}

template<typename Return, typename Para>
inline Function1Base<Return, Para>* bind(Return(*fun)(Para))
{
	return new Function1Fun<Return, Para>(fun);
}
class Test
{
public:
	int mem_fun(int a)
	{
		return a;
	}
	static int static_fun(int a)
	{
		return a;
	}
};
int main()
{
	Test f_Test;
	function1<int,int> f_fun1=bind(&Test::mem_fun, &f_Test);
	function1<int,int> f_fun2=bind(&Test::static_fun);
	assert(1 == f_fun1(1));
	assert(2 == f_fun2(2));
    return 0;
}

后来接触到function,function的接口定义让我无比膜拜,我自己实现的版本要实现拷贝,需要定义clone函数,而且内存的频繁分配,接口也不友好。而function常见使用如下:

#include <functional>
#include <assert.h>
using namespace std;
class Test
{
public:
	int mem_fun(int a)
	{
		return a;
	}
	static int static_fun(int a)
	{
		return a;
	}
	int operator()(int a)
	{
		return a;
	}
};
int main()
{
	Test f_Test;
	function<int(int)> f_fun1=bind(&Test::mem_fun, &f_Test,placeholders::_1);
	function<int(int)> f_fun2=&Test::static_fun;
	function<int(int)> f_fun3 =f_Test;
	assert(1 == f_fun1(1));
	assert(2 == f_fun2(2));
	assert(3 == f_fun3(2));
    return 0;
}
function可以是函数指针,成员函数指针,仿函数。看到int(int)时,我真是石化,这是什么玩意?,placehoders就不用说了,一直想不明白_1是什么玩意?这时候接触到刘未鹏的源码分析,感觉不明觉厉,后来modern c++ design,脑洞大开,到此,我的c++ template之门打开,但是也基本到那本书结束,不能否认元编程的快乐,但是我认为一个程序的可维护性也很重要,元编程对库作者而言是利器,但是实际项目中还是oo的天下。后来是C++11,将function收入标准库,我完全对function依赖。最近要写一个库,当库的使用者不想使用boost时,而编译器也不支持c++11或tr1时,我自已重复造轮子,把functinon实现了一遍(当然只实现了我认为对项目有用的接口),又学习到很多东西,决定写一个function的源码分析。

向刘未鹏致敬,从他博客学习颇多。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
神书-STL实现原理,对于强化数据结构-算法的程序员必备、必读书籍。The Best-Selling Programmer Resource–Now Updated for C++11 The C++ standard library provides a set of common classes and interfaces that greatly extend the core C++ language. The library, however, is not self-explanatory. To make full use of its components - and to benefit from their power - you need a resource that does far more than list the classes and their functions. The C++ Standard Library - A Tutorial and Reference, 2nd Edition describes this library as now incorporated into the new ANSI/ISO C++ language standard (C++11). The book provides comprehensive documentation of each library component, including an introduction to its purpose and design; clearly written explanations of complex concepts; the practical programming details needed for effective use; traps and pitfalls; the exact signature and definition of the most important classes and functions; and numerous examples of working code. The book focuses on the Standard Template Library (STL), examining containers, iterators, function objects, and STL algorithms. You will also find detailed coverage of strings, concurrency, random numbers and distributions, special containers, numerical classes, internationalization, and the IOStreams library. An insightful introduction to fundamental concepts and an overview of the library will help bring newcomers quickly up to speed. A comprehensive index will support the C++ programmer in his/her day-to-day life.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值