2
0
mirror of https://github.com/boostorg/lambda.git synced 2026-01-21 17:02:36 +00:00
Files
lambda/test/bll_and_function.cpp
2002-02-21 16:03:12 +00:00

65 lines
1.2 KiB
C++

// bll_and_function.cpp --------------------------------
// test using BLL and boost::function
#define BOOST_INCLUDE_MAIN // for testing, include rather than link
#include <boost/test/test_tools.hpp> // see "Header Implementation Option"
#include "boost/lambda/lambda.hpp"
#include "boost/function.hpp"
#include <vector>
#include <map>
#include <set>
#include <string>
using namespace boost::lambda;
using namespace std;
void test_function() {
boost::function<int, int, int> f;
f = unlambda(_1 + _2);
// unlambda must be used, because boost::function tries to take the
// address of the function object that is assigned. However, the
// operator& is overloaded for lambda functors, which creates a conflict
BOOST_TEST(f(1, 2)== 3);
int i=1; int j=2;
boost::function<int&, int&, int> g = unlambda(_1 += _2);
g(i, j);
BOOST_TEST(i==3);
int* sum = new int();
*sum = 0;
boost::function<int&, int> counter = unlambda(*sum += _1);
counter(5); // ok, sum* = 5;
BOOST_TEST(*sum == 5);
delete sum;
// The next statement would lead to a dangling reference
// counter(3); // error, *sum does not exist anymore
}
int test_main(int, char *[]) {
test_function();
return 0;
}