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的更多相关文章

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

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

  2. 以boost::function和boost:bind取代虚函数

    转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是“继承就像一条贼船,上去就下不 ...

  3. 关于boost::function与boost::bind函数的使用心得

    最近开始写一个线程池,期间想用一个通用的函数模板来使得各个线程执行不同的任务,找到了Boost库中的function函数. Boost::function是一个函数包装器,也即一个函数模板,可以用来代 ...

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

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

  5. 函数指针&amp;绑定: boost::functoin/std::function/bind

    see link: https://isocpp.org/wiki/faq/pointers-to-members function vs template: http://stackoverflow ...

  6. boost::function和boost:bind取代虚函数

    以boost::function和boost:bind取代虚函数 这是一篇比较情绪化的blog,中心思想是"继承就像一条贼船,上去就下不来了",而借助boost::function ...

  7. boost::bind和boost::function使用示例

    C++11已支持bind和function,之前的不支持,但可以借助boost达到同样目的.看如下两段代码: 1) 创建HDFS目录 void hdfs::init() { if (0 == hdfs ...

  8. boost库 bind/function的使用

    Boost::Function 是对函数指针的对象化封装,在概念上与广义上的回调函数类似.相对于函数指针,function除了使用自由函数,还可以使用函数对象,甚至是类的成员函数,这个就很强大了哈 # ...

  9. boost::function 通过boost::bind调用类成员函数

    1. 首先引用boost::function和boost::bind的头文件和库: #include "boost/bind.hpp" #include "boost/f ...

随机推荐

  1. .htaccess 详解

    .htaccess是什么 .htaccess文件(或者"分布式配置文件")提供了针对目录改变配置的方法, 即,在一个特定的文档目录中放置一个包含一个或多个指令的文件, 以作用于此目 ...

  2. 洛谷P1441 砝码称重(搜索,dfs+bitset优化)

    洛谷P1441 砝码称重 \(n\) 的范围为 \(n \le 20\) ,\(m\) 的范围为 \(m \le 4\) . 暴力遍历每一种砝码去除情况,共有 \(n^m\) 种情况. 对于剩余砝码求 ...

  3. 16/7/8_PHP-书写规范 PHP Coding Standard

    变量命名规范这里感觉 打算采用 匈牙利命名法+驼峰法命名,因为 PHP是弱类型语言,很多时间因为忽略了变量类型而导致犯一些低级错误.所以在前面加上类型名有助于更好的理解代码. 下载是转载 PHP书写规 ...

  4. Vue访问子组件实例或子元素

    1 尽管存在 prop 和事件,有的时候你仍可能需要在 JavaScript 里直接访问一个子组件(例如,调用子组件的方法).为了达到这个目的,你可以通过 ref 特性为这个子组件赋予一个 ID 引用 ...

  5. Maven系列学习(二)Maven使用入门

    Maven使用入门 通过上一节的学习,我们已经了解和配置好了Maven,接下来需要编写代码了 1.POM(Project Object Model,项目对象模型) 和Make的Makefile类似,M ...

  6. debain8 安装mysql8

    一.下载apt源 https://dev.mysql.com/downloads/repo/apt/ 二.更新apt sudo apt-get update 三.安装mysql sudo apt-ge ...

  7. Quartz-第二篇 使用quartz框架定时推送邮件

    1.定时推送邮件,也就是使用定时调度框架触发我们的发邮件动作,发邮件动作,请参考我的这篇随笔.

  8. jvm性能监控(5)-jdk自带工具 VisualVM

    一.在服务器的jdk的bin目录下添加配置文件 jstatd.all.policy [root@localhost /]# cd /usr/local/src/jdk1.8.0_131/bin/ [r ...

  9. 一个神奇却很简单的css特效

    在网上看到一个前端大牛的主页,觉得他有一个特效特别酷,一开始还以为是要用什么javascript代码来实现,但仔细看一下,发觉只是用几行css代码就搞定了,我觉得挺好的. 他这个效果就是鼠标放在左半部 ...

  10. java写文件UTF-8格式

    String fileName = dir + File.separator + date + File.separator + (file.list().length + 1) + ".t ...