| 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 | #define BOOST_CHECK BOOST_TEST |
| 16 | |
| 17 | #include "boost/lambda/lambda.hpp" |
| 18 | |
| 19 | #include "boost/function.hpp" |
| 20 | |
| 21 | #include <vector> |
| 22 | #include <map> |
| 23 | #include <set> |
| 24 | #include <string> |
| 25 | |
| 26 | |
| 27 | using namespace boost::lambda; |
| 28 | |
| 29 | using namespace std; |
| 30 | |
| 31 | void test_function() { |
| 32 | |
| 33 | boost::function<int (int, int)> f; |
| 34 | f = _1 + _2; |
| 35 | |
| 36 | BOOST_CHECK(f(1, 2)== 3); |
| 37 | |
| 38 | int i=1; int j=2; |
| 39 | boost::function<int& (int&, int)> g = _1 += _2; |
| 40 | g(i, j); |
| 41 | BOOST_CHECK(i==3); |
| 42 | |
| 43 | |
| 44 | |
| 45 | int* sum = new int(); |
| 46 | *sum = 0; |
| 47 | boost::function<int& (int)> counter = *sum += _1; |
| 48 | counter(5); // ok, sum* = 5; |
| 49 | BOOST_CHECK(*sum == 5); |
| 50 | delete sum; |
| 51 | |
| 52 | // The next statement would lead to a dangling reference |
| 53 | // counter(3); // error, *sum does not exist anymore |
| 54 | |
| 55 | } |
| 56 | |
| 57 | |
| 58 | int main() { |
| 59 | |
| 60 | test_function(); |
| 61 | |
| 62 | return boost::report_errors(); |
| 63 | } |
| 64 | |