首页 > 代码库 > boost::function实践——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》

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 }