1 | // bll_and_function.cpp - The Boost Lambda Library ----------------------- |
2 | // |
3 | // Copyright (C) 2000-2003 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi) |
4 | // Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com) |
5 | // |
6 | // Distributed under the Boost Software License, Version 1.0. (See |
7 | // accompanying file LICENSE_1_0.txt or copy at |
8 | // http://www.boost.org/LICENSE_1_0.txt) |
9 | // |
10 | // For more information, see www.boost.org |
11 | |
12 | // test using BLL and boost::function |
13 | |
14 | #include <boost/core/lightweight_test.hpp> |
15 | |
16 | #include "boost/lambda/lambda.hpp" |
17 | |
18 | #include "boost/function.hpp" |
19 | |
20 | #include <vector> |
21 | #include <map> |
22 | #include <set> |
23 | #include <string> |
24 | |
25 | |
26 | using namespace boost::lambda; |
27 | |
28 | using namespace std; |
29 | |
30 | void test_function() { |
31 | |
32 | boost::function<int (int, int)> f; |
33 | f = _1 + _2; |
34 | |
35 | BOOST_TEST_EQ(f(1, 2), 3); |
36 | |
37 | int i=1; int j=2; |
38 | boost::function<int& (int&, int)> g = _1 += _2; |
39 | g(i, j); |
40 | BOOST_TEST_EQ(i, 3); |
41 | |
42 | |
43 | |
44 | int* sum = new int(); |
45 | *sum = 0; |
46 | boost::function<int& (int)> counter = *sum += _1; |
47 | counter(5); // ok, sum* = 5; |
48 | BOOST_TEST_EQ(*sum, 5); |
49 | delete sum; |
50 | |
51 | // The next statement would lead to a dangling reference |
52 | // counter(3); // error, *sum does not exist anymore |
53 | |
54 | } |
55 | |
56 | |
57 | int main() |
58 | { |
59 | test_function(); |
60 | |
61 | return boost::report_errors(); |
62 | } |
63 | |