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. Linux更换jdk版本的一些问题

    111111111111111111111111111111111111111111111111111111111111111111111111 在服务器上更新了新的 jdk,也在 /etc/prof ...

  2. msyqld 的 The user specified as a definer ('root'@'%') does not exist 问题

    msyqld 的 The user specified as a definer ('root'@'%') does not exist 问题 造成问题:搭建网站时显示内容不完整. 跟踪tomcat日 ...

  3. mac 开启 chrome 和 微信开发者工具 跨域

    微信开发者工具:open -n /Applications/wechatwebdevtools.app --args --disable-web-security --user-data-dir=/U ...

  4. Resetting the Root Password Using rd.break for RHEL7

    Start the system and, on the GRUB 2 boot screen, press the e key for edit. Remove the rhgb and quiet ...

  5. 转--activemq的官方中文文档

    1 JMS 在介绍ActiveMQ之前,首先简要介绍一下JMS规范. 1.1 JMS的基本构件 1.1.1 连接工厂 连接工厂是客户用来创建连接的对象,例如ActiveMQ提供的ActiveMQCon ...

  6. git之生成SSH key

    git之生成SSH key SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定.利用 SSH 协议可以有效防止远程管理过程中的信 ...

  7. GeoServer之sqlserver插件安装

    GeoServer之sqlserver插件安装 GeoServer可以直接从sqlserver中获取数据,用来发布wms服务.但是其sqlserver连接插件并没有直接集成在Geoserver中,需要 ...

  8. 跟我学算法-tensorflow 实现神经网络

    神经网络主要是存在一个前向传播的过程,我们的目的也是使得代价函数值最小化 采用的数据是minist数据,训练集为50000*28*28 测试集为10000*28*28 lable 为50000*10, ...

  9. bootstrap 的页码显示问题-------------德州

    之前一个小bug,无论上边怎么搜索,下边的页码,不会改变 调整: 1,在mapper中添加计数, 2,找到service,queryPage中添加, 3,关键一部,如果没有会报错:,找不到该列 so, ...

  10. 在SharePoint解决方案中使用JavaScript (2) – 模块化

    本文是在SharePoint中使用JavaScript的第二篇文章,前面的文章包括: 在SharePoint解决方案中使用JavaScript (0) 在SharePoint解决方案中使用JavaSc ...