boost::function实践——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》
代码段1:
#include <boost/function.hpp>
#include <iostream> float mul_ints(int x, int y) { return ((float)x) * y; }
struct int_div {
float operator()(int x, int y) const { return ((float)x)/y; };
}; int main()
{
boost::function<float (int x, int y)> f;
f = int_div();
std::cout << f(, ) << std::endl;
if (f)
std::cout << f(, ) << std::endl;
else
std::cout << "f has no target, so it is unsafe to call" << std::endl;
f = ;
f = &mul_ints;
if (!f.empty())
{
std::cout << f(, ) << std::endl;
}
else
{
std::cout << "f has no target, so it is unsafe to call" << std::endl;
} f = boost::ref(int_div());
std::cout << f(, ) << std::endl; //error
//f = &int_div();
//std::cout << f(5, 3) << std::endl; return ;
}
代码段2:
#include <boost/function.hpp>
#include <iostream> void do_sum_avg(int values[], int n, int& sum, float& avg)
{
sum = ;
for (int i = ; i < n; i++)
sum += values[i];
avg = (float)sum / n;
}
int main()
{
//boost::function<void (int values[], int n, int& sum, float& avg)> sum_avg; //1,表意清晰
//boost::function<void (int *values, int n, int& sum, float& avg)> sum_avg; //2,同义
boost::function<void (int *, int , int& , float& )> sum_avg; //3,无参数,表意不清晰 //sum_avg = &do_sum_avg; //1,对它取指针
//sum_avg = boost::ref(do_sum_avg); //2,对它的引用
sum_avg = do_sum_avg; //3,这样写不严谨 int arr[] = {, , , , };
int cnArr = sizeof(arr)/sizeof(int);
int sum = ;
float avg = 0.0;
sum_avg(arr, cnArr, sum, avg);
std::cout << "arr, " << sum << ", " << avg << std::endl; return ;
}
代码段3:
#include <boost/function.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <iostream>
#include <functional> struct X {
int foo(int);
std::ostream& foo2(std::ostream&) const;
};
int X::foo(int x) { return -x; }
std::ostream& X::foo2(std::ostream& x) const { return x; } int main()
{
boost::function<int (X*, int)> f;
boost::function<std::ostream& (X*, std::ostream&)> f2; f = &X::foo;
//f = &boost::ref(X::foo);//error
//f = boost::ref(&X::foo);//error
f2 = &X::foo2; X x;
BOOST_TEST(f(&x, ) == -);
BOOST_TEST(f2(&x, boost::ref(std::cout)) == std::cout); return ::boost::report_errors();
}
代码段4:
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/mem_fn.hpp>
#include <iostream>
#include <functional> struct X {
int foo(int);
};
int X::foo(int x) { return -x; } int main()
{
boost::function<int (int)> f;
X x;
//f = std::bind1st(std::mem_fun(&X::foo), &x); //1 ok
//f = boost::mem_fn(boost::bind(&X::foo, &x)); //2 error
//f = std::bind1st(boost::mem_fn(&X::foo), &x); //3 ok
//f = std::bind(boost::mem_fn(&X::foo), &x); //4 error
//f = std::bind(&X::foo, &x, _1); //5 error f(); // Call x.foo(5) return ;
}
代码段5:
#include <boost/function.hpp>
#include <iostream> struct stateful_type { int operator()(int) const { return ; } }; int main()
{
stateful_type a_function_object;
boost::function<int (int)> f;
f = boost::ref(a_function_object); boost::function<int (int)> f2(f); f2.clear(); //
f2 = ; // return ;
}
代码段6:
#include <boost/test/minimal.hpp>
#include <boost/function.hpp>
#include <iostream> struct stateful_type { int operator()(int) const { return ; } }; int test_main(int, char*[])
//int main()
{
stateful_type a_function_object;
boost::function<int (int)> f;
f = boost::ref(a_function_object);//error?
BOOST_CHECK(!f.empty());
std::cout << f() << std::endl;
f.clear(); f = boost::ref(stateful_type());//ok
BOOST_CHECK(!f.empty());
std::cout << f() << std::endl;
f.clear(); //f = boost::ref(stateful_type);//error f = stateful_type();//ok
BOOST_CHECK(!f.empty());
std::cout << f() << std::endl;
f.clear(); boost::function<int (int)> f2(f); return ;
}
代码段7:
#include <boost/test/minimal.hpp>
#include <boost/function.hpp> using namespace std;
using namespace boost; static int bad_fn(float f) { return static_cast<int>(f); } int
test_main(int, char*[])
{
function0<int> f1;
f1 = bad_fn; BOOST_ERROR("This should not have compiled."); return ;
}
boost::function实践——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》的更多相关文章
- boost::bind实践2——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》
直接代码: 代码段1: #include <iostream> #include <string> #include <boost/bind/bind.hpp> c ...
- boost::bind实践
第一部分源码为基础实践: /*Beyond the C++ Standard Library ( An Introduction to Boost )[CN].chm*/ /*bind的用法*/ #i ...
- 以boost::function和boost:bind取代虚函数
转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是“继承就像一条贼船,上去就下不 ...
- boost::function和boost:bind取代虚函数
以boost::function和boost:bind取代虚函数 这是一篇比较情绪化的blog,中心思想是"继承就像一条贼船,上去就下不来了",而借助boost::function ...
- boost::function的用法
本片文章主要介绍boost::function的用法. boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象. 1. 介绍 Boost.Func ...
- boost::bind 和 boost::function 基本用法
这是一篇介绍bind和function用法的文章,起因是近来读陈硕的文章,提到用bind和function替代继承,于是就熟悉了下bind和function的用法,都是一些网上都有的知识,记录一下,期 ...
- 关于boost::function与boost::bind函数的使用心得
最近开始写一个线程池,期间想用一个通用的函数模板来使得各个线程执行不同的任务,找到了Boost库中的function函数. Boost::function是一个函数包装器,也即一个函数模板,可以用来代 ...
- [转] boost::function用法详解
http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost/fun ...
- #include <boost/function.hpp>
为atoi取别名fun,fun实质上是函数指针 #include <iostream> #include <boost/function.hpp> void main() { ...
随机推荐
- glsl-UBO
UBO 是什么?为何要用UBO? 1.数据共享设计 采用Block的原因是: 如果你的程序中包含了多个着色器,而且这些着色器使用了相同的Uniform变量,你就不得不为每个着色器分别管理这些变量.Un ...
- FileUtils类介绍
Java的文件操作太基础,缺乏很多实用工具,比如对目录的操作,支持就非常的差了.如果你经常用Java操作文件或文件夹,你会觉得反复编写这些代码是令人沮丧的问题,而且要大量用到递归. 下面是的一个解决方 ...
- 解决Scrapy抓取中文结果保存为文件时的编码问题
import json import codecs # Define your item pipelines here # # Don't forget to add your pipeline to ...
- 日常使用 Git 的 19 个建议
如果你对git一无所知,那么我建议先去读一下Git 常用命令速查.本篇文章主要适合有一定 git 使用基础的人群. 目录: 日志输出参数 查看文件的详细变更 查看文件中指定位置的变更 查看尚未合并(m ...
- chgrp、chown、chmod命令
讲解内容: 权限代号的含义如下: r:读写目录或文件的权限 read w :写入目录或文件的权限 write x :执行目录或文件的权限 -:删除目录或文件的权限 s:特殊权限,更改目录或文件 ...
- Nginx 做负载均衡的几种轮询策略
网上看见nginx的upstream目前支持的5种方式的分配,摘录备忘. 1.轮询(默认)每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除.upstream back ...
- 《神经网络和深度学习》系列文章三:sigmoid神经元
出处: Michael Nielsen的<Neural Network and Deep Leraning>,点击末尾“阅读原文”即可查看英文原文. 本节译者:哈工大SCIR硕士生 徐伟 ...
- 【JAVA - SSM】之MyBatis与原生JDBC、Hibernate访问数据库的比较
首先来看一下原生JDBC访问数据库的代码: public static void main(String[] args) { // 数据库连接 Connection connection = null ...
- android的消息处理机制(图+源码分析)——Looper,Handler,Message
android源码中包含了大量的设计模式,除此以外,android sdk还精心为我们设计了各种helper类,对于和我一样渴望水平得到进阶的人来说,都太值得一读了.这不,前几天为了了解android ...
- pcap 安装(debian7 linux) qt 使用pcap.h
安装方法一.sudo apt-get install libpcap-dev 安装方法二. 去http://www.tcpdump.org/下载最新的libpcap.tar.gz包 解压以后 ./co ...