C++11已支持bind和function,之前的不支持,但可以借助boost达到同样目的。看如下两段代码:

1) 创建HDFS目录

void hdfs::init()

{

if (0 == hdfsExists(fs, data_dirpath.c_str()))

{

LOG(INFO) << data_dirpath << " exists";

}

else

{

if (0 == hdfsCreateDirectory(fs, data_dirpath.c_str()))

{

LOG(INFO) << "create " << data_dirpath << " SUCCESS";

}

}

}

2) 创建本地目录

void local::init()

{

if (0 == access(data_dirpath.c_str(), R_OK | W_OK | X_OK))

{

LOG(INFO) << data_dirpath << " exists";

}

else

{

if (0 == hdfsCreateDirectory(data_dirpath.c_str(), S_IRWXU | S_IXGRP | S_IXOTH))

{

LOG(INFO) << "create " << data_dirpath << " SUCCESS";

}

}

}

不难看出上述两段代码逻辑是一样的,但是调用的函数名不同,而且函数的参数列表不同。下面利用boost::bind和boost::function将它们统一成一个实现:

void Xinit(boost::function<int (const char*)> exist_directory

, boost::function<int (const char*)> create_directory)

{

if (0 == exist_directory(data_dirpath.c_str()))

{

LOG(INFO) << data_dirpath << " exists";

}

else

{

if (0 == create_directory(data_dirpath.c_str()))

{

LOG(INFO) << "create " << data_dirpath << " SUCCESS";

}

}

}

void hdfs::init()

{

Xinit(boost::bind(&hdfsExists, fs, _1)

, boost::bind(&hdfsCreateDirectory, fs, _1));

}

void local::init()

{

Xinit(boost::bind(&access, _1, R_OK | W_OK | X_OK)

, boost::bind(&mkdir, _1, S_IRWXU | S_IXGRP | S_IXOTH));

}

是不是看起来很舒服了?

1) boost::function

它的模板参数为函数原型,格式为:函数返回类型 (参数列表),其中的类型还可以为模板。

2) boost:bind

它可以带多个参数,第一个参数总是为函数地址,如果为非类成员函数,则后面跟参数列表,如果是类成员函数,则第二个参数为类对象的地址。

其中“_1”和“_2”等,表示参数的占位符,对应于boost::function中的函数原型参数列表。像“fs”和“R_OK | W_OK | X_OK”,一看就知道是咋回事。

有人说可以用它来替代C++中的虚拟函数,而且比虚拟函数更优雅,但我不这么认同,实际工作中,常常两者结合使用,以达到简化代码的目的。

boost::bind和boost::function使用示例的更多相关文章

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

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

  2. 用boost::bind构造boost::coroutine

    class TestCoro { ... typedef boost::coroutines::coroutione<void ()> Coro; void CoroFun(Coro::c ...

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

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

  4. 1,Boost -> Bind

    #include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <iostream> usin ...

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

    直接代码: 代码段1: #include <iostream> #include <string> #include <boost/bind/bind.hpp> c ...

  6. boost asio 学习(二)了解boost::bind

    2.了解boost::bind使用boost::bind封装一个函数,考虑以下例子示例2a #include <iostream> #include <boost/bind.hpp& ...

  7. boost::bind的使用方法

    bind - boost 头文件: boost/bind.hpp bind 是一组重载的函数模板.用来向一个函数(或函数对象)绑定某些参数. bind的返回值是一个函数对象. 它的源文件太长了. 看不 ...

  8. boost bind使用指南

    bind - boost 头文件: boost/bind.hpp bind 是一组重载的函数模板.用来向一个函数(或函数对象)绑定某些参数. bind的返回值是一个函数对象. 它的源文件太长了. 看不 ...

  9. boost::bind四种应用场景的例子

        普通函数 int f( int a, int b ){return a + b;}boost::bind( f, _1, 9 )( 1 ) 成员函数 struct demo{int f( in ...

随机推荐

  1. 什么是 MVC ?

    本篇博客打算简单介绍一下MVC是什么,为接下来MVC的学习做一下铺垫. MVC是一种架构设计模式,是一种设计理念.是为了达到分层设计的目的,从而使代码解耦,便于维护和代码的复用.MVC是3个单词的缩写 ...

  2. windows server 2008 配置DNS服务器与IIS

    0x00: 总结这个星期在学校学的. 0x01安装: 首先你得安装好windows server 2008 然后在添加角色->安装IIS和DNS服务器 勾选好你要安装的. 安装-> 根据老 ...

  3. Java中UTC时间转换

    import java.text.SimpleDateFormat; import java.util.Date; import java util.Calendar; public class Te ...

  4. litecoin源码

    安装钱包 http://www.laiteb.com/92 莱特币基于比特币的v0.9.0rc2修改而来,从比较两者最新源码的分支图可以看出

  5. 关注下Swoole

    面向生产环境的 PHP 异步网络通信引擎 使 PHP 开发人员可以编写高性能的异步并发 TCP.UDP.Unix Socket.HTTP,WebSocket 服务.Swoole 可以广泛应用于互联网. ...

  6. python找寻合适的日志库logging Handler——Handler自定义实现

    最近在用python tornado开发一个app的服务端.投产的系统肯定需要包含日志功能,这里就自然想到了用python自带的logging库.   logging中日志内容的输出都交由Handle ...

  7. C#如何消除绘制图形缩放时抖动,总结

    一.手动双缓冲 首先定义一个BitmapBitmap backBuffer = new Bitmap(画布宽度, 画布高度);然后获取这个Bitmap的GraphicsGraphics graphic ...

  8. 「小程序JAVA实战」小程序多媒体组件(27)

    转自:https://idig8.com/2018/08/19/xiaochengxujavashizhanxiaochengxuduomeitizujian27/ 来说下 ,小程序的多媒体组件.源码 ...

  9. JDK动态代理,此次之后,永生难忘

    出自:http://www.cnblogs.com/dreamroute/p/5273888.html#3839242 首先感谢"神一样的存在"的文章! 动态代理,这个词在Java ...

  10. JAVA中Colllection的基本功能

    Collection中的add方法: 代码: public static void main(String[] args) {        // TODO Auto-generated method ...