bll_and_function.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. // test using BLL and boost::function
  12. #include <boost/test/minimal.hpp> // see "Header Implementation Option"
  13. #include "boost/lambda/lambda.hpp"
  14. #include "boost/function.hpp"
  15. #include <vector>
  16. #include <map>
  17. #include <set>
  18. #include <string>
  19. using namespace boost::lambda;
  20. using namespace std;
  21. void test_function() {
  22. boost::function<int (int, int)> f;
  23. f = _1 + _2;
  24. BOOST_CHECK(f(1, 2)== 3);
  25. int i=1; int j=2;
  26. boost::function<int& (int&, int)> g = _1 += _2;
  27. g(i, j);
  28. BOOST_CHECK(i==3);
  29. int* sum = new int();
  30. *sum = 0;
  31. boost::function<int& (int)> counter = *sum += _1;
  32. counter(5); // ok, sum* = 5;
  33. BOOST_CHECK(*sum == 5);
  34. delete sum;
  35. // The next statement would lead to a dangling reference
  36. // counter(3); // error, *sum does not exist anymore
  37. }
  38. int test_main(int, char *[]) {
  39. test_function();
  40. return 0;
  41. }