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 exist_directory
, boost::function 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++中的虚拟函数,而且比虚拟函数更优雅,但我不这么认同,实际工作中,常常两者结合使用,以达到简化代码的目的。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有