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

代码段1:


 1 #include <boost/function.hpp>
2 #include <iostream>
3
4
5 float mul_ints(int x, int y) { return ((float)x) * y; }
6 struct int_div {
7 float operator()(int x, int y) const { return ((float)x)/y; };
8 };
9
10 int main()
11 {
12 boost::function<float (int x, int y)> f;
13 f = int_div();
14 std::cout << f(5, 3) << std::endl;
15 if (f)
16 std::cout << f(5, 3) << std::endl;
17 else
18 std::cout << "f has no target, so it is unsafe to call" << std::endl;
19 f = 0;
20 f = &mul_ints;
21 if (!f.empty())
22 {
23 std::cout << f(6, 4) << std::endl;
24 }
25 else
26 {
27 std::cout << "f has no target, so it is unsafe to call" << std::endl;
28 }
29
30 f = boost::ref(int_div());
31 std::cout << f(5, 3) << std::endl;
32
33 //error
34 //f = &int_div();
35 //std::cout << f(5, 3) << std::endl;
36
37
38
39 return 0;
40 }

代码段2:


 1 #include <boost/function.hpp>
2 #include <iostream>
3
4 void do_sum_avg(int values[], int n, int& sum, float& avg)
5 {
6 sum = 0;
7 for (int i = 0; i < n; i++)
8 sum += values[i];
9 avg = (float)sum / n;
10 }
11 int main()
12 {
13 //boost::function<void (int values[], int n, int& sum, float& avg)> sum_avg; //1,表意清晰
14 //boost::function<void (int *values, int n, int& sum, float& avg)> sum_avg; //2,同义
15 boost::function<void (int *, int , int& , float& )> sum_avg; //3,无参数,表意不清晰
16
17 //sum_avg = &do_sum_avg; //1,对它取指针
18 //sum_avg = boost::ref(do_sum_avg); //2,对它的引用
19 sum_avg = do_sum_avg; //3,这样写不严谨
20
21 int arr[5] = {4, 5, 6, 9, 3};
22 int cnArr = sizeof(arr)/sizeof(int);
23 int sum = 0;
24 float avg = 0.0;
25 sum_avg(arr, cnArr, sum, avg);
26 std::cout << "arr, " << sum << ", " << avg << std::endl;
27
28 return 0;
29 }

代码段3:


 1 #include <boost/function.hpp>
2 #include <boost/detail/lightweight_test.hpp>
3 #include <iostream>
4 #include <functional>
5
6 struct X {
7 int foo(int);
8 std::ostream& foo2(std::ostream&) const;
9 };
10 int X::foo(int x) { return -x; }
11 std::ostream& X::foo2(std::ostream& x) const { return x; }
12
13 int main()
14 {
15 boost::function<int (X*, int)> f;
16 boost::function<std::ostream& (X*, std::ostream&)> f2;
17
18 f = &X::foo;
19 //f = &boost::ref(X::foo);//error
20 //f = boost::ref(&X::foo);//error
21 f2 = &X::foo2;
22
23 X x;
24 BOOST_TEST(f(&x, 5) == -5);
25 BOOST_TEST(f2(&x, boost::ref(std::cout)) == std::cout);
26
27 return ::boost::report_errors();
28 }

代码段4:


 1 #include <boost/function.hpp>
2 #include <boost/bind.hpp>
3 #include <boost/mem_fn.hpp>
4 #include <iostream>
5 #include <functional>
6
7 struct X {
8 int foo(int);
9 };
10 int X::foo(int x) { return -x; }
11
12 int main()
13 {
14 boost::function<int (int)> f;
15 X x;
16 //f = std::bind1st(std::mem_fun(&X::foo), &x); //1 ok
17 //f = boost::mem_fn(boost::bind(&X::foo, &x)); //2 error
18 //f = std::bind1st(boost::mem_fn(&X::foo), &x); //3 ok
19 //f = std::bind(boost::mem_fn(&X::foo), &x); //4 error
20 //f = std::bind(&X::foo, &x, _1); //5 error
21
22 f(5); // Call x.foo(5)
23
24 return 0;
25 }

代码段5:


 1 #include <boost/function.hpp>
2 #include <iostream>
3
4
5 struct stateful_type { int operator()(int) const { return 0; } };
6
7 int main()
8 {
9 stateful_type a_function_object;
10 boost::function<int (int)> f;
11 f = boost::ref(a_function_object);
12
13 boost::function<int (int)> f2(f);
14
15 f2.clear(); //1
16 f2 = 0; //2
17
18 return 0;
19 }

代码段6:


 1 #include <boost/test/minimal.hpp>
2 #include <boost/function.hpp>
3 #include <iostream>
4
5
6 struct stateful_type { int operator()(int) const { return 0; } };
7
8 int test_main(int, char*[])
9 //int main()
10 {
11 stateful_type a_function_object;
12 boost::function<int (int)> f;
13 f = boost::ref(a_function_object);//error?
14 BOOST_CHECK(!f.empty());
15 std::cout << f(5) << std::endl;
16 f.clear();
17
18 f = boost::ref(stateful_type());//ok
19 BOOST_CHECK(!f.empty());
20 std::cout << f(5) << std::endl;
21 f.clear();
22
23 //f = boost::ref(stateful_type);//error
24
25 f = stateful_type();//ok
26 BOOST_CHECK(!f.empty());
27 std::cout << f(5) << std::endl;
28 f.clear();
29
30 boost::function<int (int)> f2(f);
31
32 return 0;
33 }

代码段7:


 1 #include <boost/test/minimal.hpp>
2 #include <boost/function.hpp>
3
4 using namespace std;
5 using namespace boost;
6
7 static int bad_fn(float f) { return static_cast<int>(f); }
8
9 int
10 test_main(int, char*[])
11 {
12 function0<int> f1;
13 f1 = bad_fn;
14
15 BOOST_ERROR("This should not have compiled.");
16
17 return 0;
18 }

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

时间: 2024-12-19 14:37:07

boost::function实践——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》的相关文章

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

直接代码: 代码段1: 1 #include <iostream> 2 #include <string> 3 #include <boost/bind/bind.hpp> 4 5 class some_class 6 { 7 public: 8 typedef void result_type; 9 void print_string(const std::string& s) const 10 { std::cout << s <<

boost::bind实践

第一部分源码为基础实践: 1 /*Beyond the C++ Standard Library ( An Introduction to Boost )[CN].chm*/ 2 /*bind的用法*/ 3 4 #include <iostream> 5 #include <algorithm> 6 #include <functional> 7 #include <vector> 8 9 #include <boost/bind/bind.hpp&g

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

转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是"继承就像一条贼船,上去就下不来了",而借助boost::function和boost::bind,大多数情况下,你都不用上贼船. boost::function和boost::bind已经纳入了std::tr1,这或许是C++0x最值得期待的功能,它将彻底改变C++库的设计方式,以及应用程序的编写方式. Scott

Beyond the C++ Standard Library中文版pdf

下载地址:网盘下载 内容简介  · · · · · · Introducing the Boost libraries: the next breakthrough in C++ programming Boost takes you far beyond the C++ Standard Library, making C++ programming more elegant, robust, and productive. Now, for the first time, a leading

boost::bind boost::function

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 #include <boost/bind.hpp> #include <boost/function.hpp> #include <iostream&g

boost::function 介绍

本片文章主要介绍boost::function的用法. boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象. 1.  介绍 Boost.Function 库包含了一个类族的函数对象的包装.它的概念很像广义上的回调函数.其有着和函数指针相同的特性但是又包含了一个调用的接口.一个函数指针能够在能以地方被调用或者作为一个回调函数.boost.function能够代替函数指针并提供更大的灵活性. 2. 使用 Boost.Function 有两种形式

#include &lt;boost/function.hpp&gt;

为atoi取别名fun,fun实质上是函数指针 1 #include <iostream> 2 #include <boost/function.hpp> 3 4 void main() 5 { 6 boost::function<int(char *)>fun = atoi;//为atoi取别名fun,fun实质上是函数指针 7 8 std::cout << fun("123") + fun("234") <&

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

最近开始写一个线程池,期间想用一个通用的函数模板来使得各个线程执行不同的任务,找到了Boost库中的function函数. Boost::function是一个函数包装器,也即一个函数模板,可以用来代替拥有相同返回类型,相同参数类型,以及相同参数个数的各个不同的函数. 1 #include<boost/function.hpp> 2 #include<iostream> 3 typedef boost::function<int(int ,char)> Func; 4

[转] boost::function用法详解

http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost/function.hpp", 或者某个带数字的版本,从 "boost/function/function0.hpp" 到 "boost/function/function10.hpp". 如果你知道你想保存在 function 中的函数的参数数量,这样做可以让编译器