1 // Boost.Function library examples
2
3 // Copyright Douglas Gregor 2001-2003. Use, modification and
4 // distribution is subject to the Boost Software License, Version
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7
8 // For more information, see http://www.boost.org
9
10 #include <iostream>
11 #include <boost/function.hpp>
12
do_sum_avg(int values[],int n,int & sum,float & avg)13 void do_sum_avg(int values[], int n, int& sum, float& avg)
14 {
15 sum = 0;
16 for (int i = 0; i < n; i++)
17 sum += values[i];
18 avg = (float)sum / n;
19 }
20
21 int
main()22 main()
23 {
24 // The second parameter should be int[], but some compilers (e.g., GCC)
25 // complain about this
26 boost::function<void (int*, int, int&, float&)> sum_avg;
27
28 sum_avg = &do_sum_avg;
29
30 int values[5] = { 1, 1, 2, 3, 5 };
31 int sum;
32 float avg;
33 sum_avg(values, 5, sum, avg);
34
35 std::cout << "sum = " << sum << std::endl;
36 std::cout << "avg = " << avg << std::endl;
37 return 0;
38 }
39