代码段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 )》的更多相关文章

  1. boost::bind实践2——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》

    直接代码: 代码段1: #include <iostream> #include <string> #include <boost/bind/bind.hpp> c ...

  2. boost::bind实践

    第一部分源码为基础实践: /*Beyond the C++ Standard Library ( An Introduction to Boost )[CN].chm*/ /*bind的用法*/ #i ...

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

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

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

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

  5. boost::function的用法

    本片文章主要介绍boost::function的用法. boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象. 1.  介绍 Boost.Func ...

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

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

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

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

  8. [转] boost::function用法详解

    http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost/fun ...

  9. #include <boost/function.hpp>

    为atoi取别名fun,fun实质上是函数指针 #include <iostream> #include <boost/function.hpp> void main() { ...

随机推荐

  1. 城市连动纯js代码DEMO

    前台代码 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" co ...

  2. Linux,实时获取磁盘空间

    #include <iostream> #include <stdlib.h> #include <stdio.h> #include <sys/statfs ...

  3. 天涯html&css基础框架

    html申明 对ie6-8在html中添加no-css3这个class和各个版本的class,对ie7以下添加lte7这个class,然后根据我们目前使用浏览器的比例,把第一的ie6放在判断第一位. ...

  4. HTML头部<head>学习

    元素是所有头部元素的容器. 元素包含了所有的头部标签元素.在 元素中你可以插入脚本(scripts), 样式文件(CSS),及各种meta信息. 以下标签都可以添加到 head 部分: 1.title ...

  5. git push 报错

    git push报错误: Git push error: RPC failed; result=56, HTTP code = 200 fatal: The remote end hung up un ...

  6. JQuery- 动画与效果

    这几天做网站,刚好用到! 1.基本效果 匹配元素从左上角开始变浓变大或缩小到左上角变淡变小 ①隐藏元素 除了可以设置匹配元素的display:none外,可以用以下函数 hide(speed,[cal ...

  7. PHP环境配置综合篇

    1.WNMP: http://www.wnmp.com.cn/     En: https://www.getwnmp.org/ 2.xampp:https://www.apachefriends.o ...

  8. poj 3294 Life Forms

    后缀数组的题目,把后缀连接起来,这个还是先二分答案,然后选取一段连续的height值,判断这些height代表的后缀有没有覆盖一半以上的字符串. 得出答案的长度之后还要在枚举连续的heigh,判断有没 ...

  9. 懂,你的App生,不懂,死!

    近期有一些开发人员.创业公司的人加我微信viyi88,咨询一些关于自己App的事情.被问得最多的可能就是:"我的App怎样推广添加下载量?"而且信誓旦旦地说自己的App做得非常好, ...

  10. java转义xml中的多余尖括号

    xml中的敏感字符是尖括号,如果xml的值中含有尖括号,那么在解析的时候就会报错,如: <?xml version="1.0" encoding="UTF-8&qu ...