#include <iostream>
#include <string>
#include <boost\bind.hpp> using namespace std;
using namespace boost; int func(int x, int y)
{
return x + y;
} struct struct_func
{
int func(int x, int y)
{
return x*y;
}
}; int main(int argc, char *argv[])
{
// 绑定普通函数
auto ref = boost::bind(func, 20, 10)();
cout << "绑定调用: " << ref << endl; // 绑定函数指针
typedef decltype(&func) f_type;
f_type ptr_a = func;
int x = 100, y = 200;
cout << "绑定调用: " << boost::bind(ptr_a, _1, _2)(x, y) << endl;
cout << "传入单参数: " << boost::bind(ptr_a, _1, 20)(10) << endl; // 绑定成员函数
struct_func ptr_b;
auto struct_ref = boost::bind(&struct_func::func, ptr_b, _1, _2)(10, 10);
cout << "绑定调用: " << struct_ref << endl; getchar();
}

function 参数绑定

#include <iostream>
#include <string>
#include <boost\function.hpp>
#include <boost\bind.hpp> using namespace std; float MyFunc(int x, int y)
{
return x + y;
} struct MyStruct
{
int add(int x, int y)
{
return x *y;
}
}; int main(int argc,char *argv[])
{
// function 指向普通函数
boost::function<float(int, int)> function_ptr; function_ptr = MyFunc; // 将MyFunc用ptr来存储
if (function_ptr)
{
cout << "调用指针: " << function_ptr(10, 20) << endl;
}
function_ptr = 0; // function 指向成员函数
boost::function<int(int, int)> struct_ptr;
MyStruct sc; struct_ptr = boost::bind(&MyStruct::add,&sc, _1, _2);
cout <<"调用指针: " << struct_ptr(10, 20) << endl;
getchar();
}

ref库的使用

#include <iostream>
#include <string>
#include <vector>
#include <boost\bind.hpp>
#include <boost\function.hpp> using namespace std; template<typename T>
struct summary
{
typedef void result_type;
T sum; summary(T v = T()) : sum(v){}
void operator()(T const &x)
{
sum += x;
}
}; int main(int argc, char *argv[])
{
vector<int> vect = { 1, 3, 5, 7, 9 };
summary<int> s; // 定义有状态函数对象 boost::function<void(int const&)> func(ref(s)); // function 包装引用 std::for_each(vect.begin(), vect.end(), func);
cout << "求和结果: " << s.sum << endl; getchar();
}

使用普通回调函数

#include <iostream>
#include <string>
#include <vector>
#include <boost\bind.hpp>
#include <boost\function.hpp> using namespace std; // 定义回调函数
void call_back_func(int x)
{
cout << "执行回调函数(数值翻倍): " << x * 2 << endl;
} class MyClass
{
private:
typedef boost::function<void(int)> func_ptr; // function 类型定义
func_ptr func;
int n; public:
// 定义构造函数
MyClass(int i) :n(i){} // 存储回调函数
template<typename CallBack>
void accept(CallBack call)
{
func = call;
}
// 运行函数
void run()
{
func(n);
}
}; int main(int argc, char *argv[])
{
MyClass ptr(10); ptr.accept(call_back_func); // 传入回调函数
ptr.run(); getchar();
}

带状态的回调函数,ref库传递引用

#include <iostream>
#include <string>
#include <vector>
#include <boost\bind.hpp>
#include <boost\function.hpp> using namespace std; class MyClass
{
private:
typedef boost::function<void(int)> func_ptr; // function 类型定义
func_ptr func;
int n; public:
// 定义构造函数
MyClass(int i) :n(i){} // 存储回调函数
template<typename CallBack>
void accept(CallBack call)
{
func = call;
}
// 运行函数
void run()
{
func(n);
}
}; class call_back_obj
{
private:
int x; public:
call_back_obj(int i) :x(i){}
void operator()(int i)
{
cout << "回调函数: " << i * x << endl;
}
}; int main(int argc, char *argv[])
{
MyClass ptr(10);
call_back_obj call_obj(2); ptr.accept(ref(call_obj)); ptr.run();
ptr.run(); getchar();
}

通过类绑定多个callback

#include <iostream>
#include <string>
#include <vector>
#include <boost\bind.hpp>
#include <boost\function.hpp> using namespace std; class MyClass
{
private:
typedef boost::function<void(int)> func_ptr; // function 类型定义
func_ptr func;
int n; public:
// 定义构造函数
MyClass(int i) :n(i){} // 存储回调函数
template<typename CallBack>
void accept(CallBack call)
{
func = call;
}
// 运行函数
void run()
{
func(n);
}
}; class call_back_factory
{
public:
void call_back_func_a(int x)
{
cout << "回调函数1: " << x * 2 << endl;
} void call_back_func_b(int x, int y)
{
cout << "回调函数2: " << x * y << endl;
}
}; int main(int argc, char *argv[])
{
MyClass ptr(10);
call_back_factory factory; ptr.accept(bind(&call_back_factory::call_back_func_a, factory, _1));
ptr.run(); ptr.accept(bind(&call_back_factory::call_back_func_b, factory, _1, 200));
ptr.run();
getchar();
}

信号与槽 一个信号关联多个槽,信号发出后,槽函数相应。

#include <iostream>
#include <string>
#include <boost\signals2.hpp> using namespace std; void slots_a()
{
cout << "slots_a called" << endl;
} void slots_b()
{
cout << "slots_b called" << endl;
} int main(int argc, char *argv[])
{ // 简单的链接
boost::signals2::signal<void()> sig; // 定义信号对象 sig.connect(&slots_a);
sig.connect(&slots_b); sig(); // 发射信号 getchar();
}

信号的返回值

#include <iostream>
#include <string>
#include <boost\signals2.hpp> using namespace std; template<int T,int C>
struct slots
{
int operator()(int x)
{
return x + T + C;
}
}; int main(int argc, char *argv[])
{ boost::signals2::signal<int(int)> sig; // 0 代表组
sig.connect(0,slots<10,20>()); int ref = *sig(5);
cout << "获取返回值: " << ref << endl; getchar();
}

合并器的使用

#include <iostream>
#include <string>
#include <numeric>
#include <boost\signals2.hpp> using namespace std; template<int T, int C>
struct slots
{
int operator()(int x)
{
return x + T + C;
}
}; template<typename T>
class combiner
{
T v; public:
typedef std::pair<T, T> result_type;
combiner(T t = T()) : v(t){} // 构造函数 template<typename InputIterator>
result_type operator()(InputIterator begin, InputIterator end) const
{
// 为空则返回0
if (begin == end)
return result_type(); vector<T> vec(begin, end); // 容器保存插槽调用结果 T sum = std::accumulate(vec.begin(), vec.end(), v);
T max = *std::max_element(vec.begin(), vec.end()); return result_type(sum, max);
}
}; int main(int argc, char *argv[])
{
boost::signals2::signal<int(int), combiner<int>> sig; sig.connect(0, slots<10, 20>()); auto x = sig(2);
cout << x.first << x.second << endl;;
getchar();
}

C++ Boost 函数与回调应用的更多相关文章

  1. boost 函数与回调

    result_of 含义:result_of可以帮助程序员确定一个调用表达式的返回类型,主要用于泛型编程和其他boost库组件,它已经被纳入TR1 头文件:<boost/utility/resu ...

  2. php变量函数,回调函数

    一,变量可以直接传递函数 <?php function demo($num , $n )//$n是个函数 { for($i=0;$i<$num;++$i) { if($n($i)) { e ...

  3. paip.函数方法回调机制跟java php python c++的实现

    paip.函数方法回调机制跟java php python c++的实现 作者Attilax 艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http:// ...

  4. C++中类成员函数作为回调函数

    注:与tr1::function对象结合使用,能获得更好的效果,详情见http://blog.csdn.net/this_capslock/article/details/38564719 回调函数是 ...

  5. Go基础系列:函数(2)——回调函数和闭包

    回调函数和闭包 当函数具备以下两种特性的时候,就可以称之为高阶函数(high order functions): 函数可以作为另一个函数的参数(典型用法是回调函数) 函数可以返回另一个函数,即让另一个 ...

  6. 精读JavaScript模式(五),函数的回调、闭包与重写模式

    一.前言 今天地铁上,看到很多拖着行李箱的路人,想回家了. 在上篇博客结尾,记录到了函数的几种创建方式,简单说了下创建差异,以及不同浏览器对于name属性的支持,这篇博客将从第四章函数的回调模式说起. ...

  7. C++中 线程函数为静态函数 及 类成员函数作为回调函数

    线程函数为静态函数: 线程控制函数和是不是静态函数没关系,静态函数是在构造中分配的地址空间,只有在析构时才释放也就是全局的东西,不管线程是否运行,静态函数的地址是不变的,并不在线程堆栈中static只 ...

  8. Javascript中的回调函数和匿名函数的回调

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. Node.js学习笔记(3)——关于回调函数和函数的回调

    说明:本人是node.js的初学者,尝试向别人解释这是怎么回事是自我学习的一个好方法.如果你发现有些地方并不是那么正确,欢迎提出来让我知道以便修正,共同进步,谢过^_^.       欢迎交流,本人微 ...

  10. Javascript中的回调函数和匿名函数的回调示例介绍

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

随机推荐

  1. 【C++库函数】stringstream-类型转换&&字符分割

    继续填坑٩(•̤̀ᵕ•̤́๑),这次是 stringstream的内容,最初是看到它可以把字符串直接输出成int类型,惊了,但是一直不是很懂.在网上查了很多资料,才终于差不多理解.stringstre ...

  2. 数字孪生 3D 风电场,智慧风电之海上风电

    前言 截止 2021 年,全球已有 127 个国家做出了"碳中和"的承诺,能源低碳转型和实现碳中和已经成为全球共同的战略目标.根据权威机构预测,到 2050 年,可再生能源发电将占 ...

  3. 十一、docker的容器互联

    系列导航 一.docker入门(概念) 二.docker的安装和镜像管理 三.docker容器的常用命令 四.容器的网络访问 五.容器端口转发 六.docker数据卷 七.手动制作docker镜像 八 ...

  4. 【驱动】ifconfig up后内核网络驱动做了什么.md

    背景 最近在排查一个网络问题,ifconfig eth0 up 后,网卡link up比较慢.因此,分析了下从ifconfig up 到网络驱动的调用流程.这里顺便作个记录. ifconfig eth ...

  5. oracle数据库锁表后的处理方案

    oracle数据库, 数据库(执行查询语句或存储过程执行)操作,导致表被锁死的情况的解决方案: 一.查看已经锁定的数据库表 select b.owner,b.object_name,a.session ...

  6. (已解决)pulse secure 连接功能变灰禁用 连接面板找不到

    今天打开 pulse secure 时,发现窗口变成了这样: 连接功能是灰色的,被禁用了: 解决方案: 运行 PulseSecureService 服务. 然后就正常了!

  7. http连接池配置及spring boot restTemplate配置http连接池

    本文为博主原创,转载请注明出处: 项目中存在第三方系统之间的服务调用通信,且会进行频繁调用,由于很早之前实现的调用方式为每调用一次外部接口,就需要新建一个HttpClient 对象.由于频繁调用,会存 ...

  8. IDEA控制台输出中文乱码

    1.问题 如下图,我使用的文件编码格式为UFT-8,这里会出现中文乱码的问题. 且我并不方便直接修改全局文件编码格式,有可能会造成未知错误. 2.解决 参考链接:IDEA 控制台中文乱码 4 种解决方 ...

  9. Spring——静态/动态代理模式

    代理模式 代理模式: 静态代理 动态代理 学习aop之前,要先了解代理模式 静态代理 抽象角色:一般使用接口或者抽象类来实现 真实角色:被代理的角色 代理角色:代理真实角色:代理真实角色后,一般会做一 ...

  10. SkyWalking的学习之三

    SkyWalking的学习之三 持续优化 SkyWalking 默认可以使用h2,但是感觉容量和性能都可能不太好 所以我想使用一下elasticSearch进行替换. 自己其实一直想心想去学习, 但是 ...