代码段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. poj 1265 Area(Pick定理)

    Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5666   Accepted: 2533 Description ...

  2. ubuntu12.04下使用qemu模拟mips处理器安装debian

    注:ubuntu是不支持mips处理器的,只能在x86下安装运行第一步.安装qemu sudo apt-get install qemu qemu-system .执行 qemu-system-mip ...

  3. C语言求x的y次方,自定义函数,自己的算法

    我是一名高二中学生,初中时接触电脑,非常酷爱电脑技术,自己百度学习了有两年多了,编程语言也零零散散的学习了一点,想在大学学习计算机专业,所以现在准备系统的学习C语言,并在博客中与大家分享我学习中的心得 ...

  4. hdoj 1150 Machine Schedule【匈牙利算法+最小顶点覆盖】

    Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. 【python自动化第七篇:面向对象进阶】

    知识点概览: 静态方法,类方法,属性方法 类的特殊方法 反射 异常处理 socket开发基础 一.静态方法:@staticmethod 只是名义上归类管理,实际上在静态方法里访问不了类或者实例中的任何 ...

  6. Jsp学习(2)

    Jsp的三大指令 (1).include 作用:相当于把当前页面去包含页面 语法: <%@include file="/common/test.jsp" %> 实例如下 ...

  7. jquerymobile知识点三:弹出层popup

    弹出层popup很简单,主要就是弹出验证,登陆注册,提交信息之类的,下面是我写好的一个demo... <div data-role="popup" id="popu ...

  8. C# 枚举 字符串 转换

    普通方法 这种方法尽管很SB但确实可以解决问题 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { st ...

  9. 使用Inputstream读取文件

    在java中,能够使用InputStream对文件进行读取,就是字节流的输入.当读取文件内容进程序时,须要使用一个byte数组来进行存储,如此会有例如以下两个问题: 1.怎样建立合适大小的byte数组 ...

  10. LabVIEW的错误簇以及错误处理函数

    我们可以在LabVIEW的Modern>>Array, Matrix & Cluster控件面板找到表示错误簇数据类型的错误输入(Error In)以及错误输出(Error Out ...