boost function bind ref
boost::function to encapsulate function pointers.
1. function
#include <boost/function.hpp>
#include <iostream>
#include <cstdlib>
#include <cstring> int main()
{
boost::function<int(const char*)> f = std::atoi;
std::cout << f("") << std::endl;
f = std::strlen;
std::cout << f("") << std::endl;
return ;
}
boost::function makes it possible to define a pointer to a function with a specific signature. Example above defines a pointer f that can point to functions that expecct a parameter of type const char* and return a value of type int. Once defined, functions with matching signatures can be assigned to the pointer. Please note that types do not need to match exactly. Even though std::strlen() uses std::size_t as its return value, it can still be assigned to f.
Because f is a function pointer, the assigned function can be called using operator(). Depending on what function is currently assigned, either std::atoi() or std::strlen() is called.
Assignint nullptr to a function pointer of type boost::function releases any currently assigned function. Calling it after it has been released will result in a boost::bad_function_call exception being thrown. To check whether or not a function pointer is currently assigned to a function, you can use the member functions empty() or operator bool.
2. bind a class member function to boost::function
#include <boost/function.hpp>
#include <functional>
#include <iostream> struct world
{
void hello(std::ostream &os)
{
os << "Hello, world!\n";
}
}; int main()
{
boost::function<void(world*, std::ostream&)> f = &world::hello;
world w;
f(&w, std::ref(std::cout));
return ;
}
When calling such a function, the first parameter passed indicates the particular object for which the function is called. Therefore, the first parameter after the open parenthesis inside the template definition must be a pointer to that particular class. The remaining parameters denote the signature of the corresponding memebr function.
3. boost::bind()
#include <boost/bind.hpp>
#include <vector>
#include <algorithm>
#include <iostream> void print(std::ostream *os, int i)
{
*os << i << std::endl;
} int main()
{
std::vector<int> v{, , };
std::for_each(v.begin(), v.end(), boost::bind(print, &std::cout, _1));
std::sort(v.begin(), v.end(), boost::bind(compare, _1, _2));
return ;
}
Example uses print() as a function, not as a function object. Because print() expects two parameters, teh function can't be passed directly to std::for_each(). Instead, boost::bind() is passed to std::for_each() and print() is passed as the first parameter to boost::bind(). _1 is a placeholder. Boost.Bind defines placeholders from _1 to _9. These placeholders tell boost::bind() to return a function object that expects as many parameters as the placeholder with the greatest number. boost::bind() returns an unary function object - a function object that expects a sole parameter.
4. Boost.Ref provides two functions, boost::ref() and boost::cref(). Because std::bind() takes parameters by value, you have to deal with references explicityl.
#include <boost/ref.hpp>
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream> void print(std::ostream &os, int i)
{
os << i << std::endl;
} int main()
{
std::vector<int> v{, , };
std::for_each(v.begin(), v.end(), std::bind(print, boost::ref(std::cout), std::placeholders::_1));
return ;
}
boost::ref() is used to wrap std::cout. boost::ref() returns a proxy object that contains a reference to the object passed to it. This makes it possible to pass a reference to std::cout even though std::bind() takes all parameters by value.
The function template boost::cref() lets you pass a const reference.
boost function bind ref的更多相关文章
- boost::bind 和 boost::function 基本用法
这是一篇介绍bind和function用法的文章,起因是近来读陈硕的文章,提到用bind和function替代继承,于是就熟悉了下bind和function的用法,都是一些网上都有的知识,记录一下,期 ...
- 以boost::function和boost:bind取代虚函数
转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是“继承就像一条贼船,上去就下不 ...
- 关于boost::function与boost::bind函数的使用心得
最近开始写一个线程池,期间想用一个通用的函数模板来使得各个线程执行不同的任务,找到了Boost库中的function函数. Boost::function是一个函数包装器,也即一个函数模板,可以用来代 ...
- [置顶] 编程模仿boost::function和boost::bind
boost::function和boost::bind结合使用是非常强大的,他可以将成员函数和非成员函数绑定对一个对象上,实现了类似C#的委托机制.委托在许多时候可以替代C++里面的继承,实现对象解耦 ...
- 函数指针&绑定: boost::functoin/std::function/bind
see link: https://isocpp.org/wiki/faq/pointers-to-members function vs template: http://stackoverflow ...
- boost::function和boost:bind取代虚函数
以boost::function和boost:bind取代虚函数 这是一篇比较情绪化的blog,中心思想是"继承就像一条贼船,上去就下不来了",而借助boost::function ...
- boost::bind和boost::function使用示例
C++11已支持bind和function,之前的不支持,但可以借助boost达到同样目的.看如下两段代码: 1) 创建HDFS目录 void hdfs::init() { if (0 == hdfs ...
- boost库 bind/function的使用
Boost::Function 是对函数指针的对象化封装,在概念上与广义上的回调函数类似.相对于函数指针,function除了使用自由函数,还可以使用函数对象,甚至是类的成员函数,这个就很强大了哈 # ...
- boost::function 通过boost::bind调用类成员函数
1. 首先引用boost::function和boost::bind的头文件和库: #include "boost/bind.hpp" #include "boost/f ...
随机推荐
- flask中获取request的参数的方法
request请求总体分为两类: 1.get请求 访问时会在地址栏直接显示参数不安全,且参数大小比较小. 2.post请求 参数不显示在地址栏,一般用户注册.登录都通过post请求完成. flask获 ...
- WingIIDE 6的licese破解方法(支持python3)
(1) 安装WingIDE成功后启动,激活时输入license id CN123-12345-12345-12345 (2)点击Continue后弹框,拷贝框中的request code(将其放入脚本 ...
- Delphi保存网页中的图片
WEBBrowser已经打开了URL V = WEBBrowser.Document.body.createControlRange(); V1 = WEBBrow ...
- spss如何选择需要的变量?
spss如何选择需要的变量? 今天一位网友问我,spss如何在许多字段(变量)中选择我需要的字段,而不显示其他的字段呢? 这个问题问的很好,在实际的数据分析或者挖掘的过程中,都需要用这个来找出对商业问 ...
- 【ABAP系列】SAP ABAP基础-录制BDC的MODE定义解析
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP基础-录制BDC ...
- Convolutional Neural Networks(3):Convolution and Channels
在CNN(1)和CNN(2)两篇文章中,主要说明的是CNN的基本架构和权值共享(Weight Sharing),本文则重点介绍卷积的部分. 首先,在卷积之前,我们的数据是4D的tensor(width ...
- git统计提交次数
git log --since="Oct 27 9:16:10 2017 +0800" --pretty=oneline | wc -l
- 移动app云测试平台
一:移动App云测试平台 1.云测试平台背景 随着智能手机的普及率和渗透率越来越高,App开发软件也越来越多.但是因为安卓和IOS的碎片化,尤其是安卓,因为完全开源的原因,导致设备繁多,品牌众多,版本 ...
- bjsxt学习笔记:Dubbo
一.Dubbo诞生背景(摘自Dubbo官网-入门-背景) 二.Dubbo架构图(摘自Dubbo官网-入门-架构) 三.Dubbo核心依赖(jar包):dubbo.zkclient 四.Dubbo项目搭 ...
- [HDU5807] [BestCoder Round #86 1004] Keep In Touch (DP)
[HDU5807] [BestCoder Round #86 1004] Keep In Touch (DP) 题面 有三个人从一张N个点无重边的有向无环图上的三个点出发,每单位时间,他们分别选择当前 ...