int main(void) { //[transform int offset = 5; std::vector<int> v; std::vector<int> w; for(int i = 1; i <= 2; ++i) v.push_back(i * 10); BOOST_TEST(v[0] == 10); BOOST_TEST(v[1] == 20); w.resize(v.size()); int BOOST_LOCAL_FUNCTION(const bind& offset, int i) { return ++i + offset; } BOOST_LOCAL_FUNCTION_NAME(inc) std::transform(v.begin(), v.end(), w.begin(), inc); BOOST_TEST(w[0] == 16); BOOST_TEST(w[1] == 26); int BOOST_LOCAL_FUNCTION(bind& inc, int i, int j) { return inc(i + j); // Call the other bound local function. } BOOST_LOCAL_FUNCTION_NAME(inc_sum) offset = 0; std::transform(v.begin(), v.end(), w.begin(), v.begin(), inc_sum); BOOST_TEST(v[0] == 27); BOOST_TEST(v[1] == 47); //] return boost::report_errors(); }
int main(void) { std::string message = "abc"; // Reference valid where closure used. void BOOST_LOCAL_FUNCTION(bind& message, const std::string& text) { message = text; } BOOST_LOCAL_FUNCTION_NAME(s) set = s; const std::string& BOOST_LOCAL_FUNCTION(const bind& message) { return message; } BOOST_LOCAL_FUNCTION_NAME(g) get = g; action(); return boost::report_errors(); }
int main(void) { int x = 0; void BOOST_LOCAL_FUNCTION( (bind& x) ) { void BOOST_LOCAL_FUNCTION( (bind& x) ) { x++; } BOOST_LOCAL_FUNCTION_NAME(g) x--; g(); } BOOST_LOCAL_FUNCTION_NAME(f) f(); BOOST_TEST(x == 0); return boost::report_errors(); }
int main(void) { //[add_default int BOOST_LOCAL_FUNCTION(int x, int y, default 2) { // Default parameter. return x + y; } BOOST_LOCAL_FUNCTION_NAME(add) BOOST_TEST(add(1) == 3); //] return boost::report_errors(); }
int main(void) { //[add_classifiers int BOOST_LOCAL_FUNCTION(auto int x, register int y) { // Classifiers. return x + y; } BOOST_LOCAL_FUNCTION_NAME(add) //] BOOST_TEST(add(1, 2) == 3); return boost::report_errors(); }
int main(void) { //[add_params_only int BOOST_LOCAL_FUNCTION(int x, int y) { // Local function. return x + y; } BOOST_LOCAL_FUNCTION_NAME(add) BOOST_TEST(add(1, 2) == 3); // Local function call. //] return boost::report_errors(); }
int main(void) { int BOOST_LOCAL_FUNCTION(int x) { return x + 4; } BOOST_LOCAL_FUNCTION_NAME(add2) boost::function<int (int)> a2 = add2; // Reference valid where closure used. boost::function<int (int)> d2 = derivative(a2, 2); BOOST_TEST(d2(6) == 1); return boost::report_errors(); }
int main(void) { //[nesting int x = 0; void BOOST_LOCAL_FUNCTION(bind& x) { void BOOST_LOCAL_FUNCTION(bind& x) { // Nested. x++; } BOOST_LOCAL_FUNCTION_NAME(g) x--; g(); // Nested local function call. } BOOST_LOCAL_FUNCTION_NAME(f)
int main(void) { int nums[] = {1, 2, 3}; int offset = -1; int BOOST_LOCAL_FUNCTION(const bind offset, int* array, int index) { return array[index + offset]; } BOOST_LOCAL_FUNCTION_NAME(access) BOOST_TEST(access(nums, 1) == 1); BOOST_TEST(access(nums, 2) == 2); BOOST_TEST(access(nums, 3) == 3); return boost::report_errors(); }
T calculate(const T& factor) { T sum = 0; void BOOST_LOCAL_FUNCTION_TPL( (const bind factor) (bind& sum) (T num) ) { BOOST_CONCEPT_ASSERT((Addable<typename boost::remove_reference< BOOST_LOCAL_FUNCTION_TYPEOF(sum)>::type>)); sum += factor * num; } BOOST_LOCAL_FUNCTION_NAME(add) add(6); return sum; }
int main(void) { //[operator_error bool BOOST_LOCAL_FUNCTION(const point& p, const point& q) { return p.x == q.x && p.y == q.y; } BOOST_LOCAL_FUNCTION_NAME(operator==) // Error: Cannot use `operator...`. //] point a; a.x = 1; a.y = 2; point b = a; BOOST_TEST(a == b); return boost::report_errors(); }
T total(const T& x, const T& y, const T& z) { T sum = T(), factor = 10; // Using the `..._TPL` macro. T BOOST_LOCAL_FUNCTION_TPL(const bind factor, bind& sum, T num) { return sum += factor * num; } BOOST_LOCAL_FUNCTION_NAME(add) add(x); T nums[2]; nums[0] = y; nums[1] = z; std::for_each(nums, nums + 2, add); return sum; }
boost::function<int (int, int)> linear(const int& slope) { int BOOST_LOCAL_FUNCTION(const bind& slope, int x, default 1, int y, default 2) { return x + slope * y; } BOOST_LOCAL_FUNCTION_NAME(lin) boost::function<int (int, int)> f = lin; // Assign to local variable. BOOST_TEST(f(1, 2) == 5); call1(lin); // Pass to other functions. call0(lin); return lin; // Return. }
int main(void) { //[macro_commas void BOOST_LOCAL_FUNCTION( BOOST_IDENTITY_TYPE((const std::map<std::string, size_t>&)) m, BOOST_IDENTITY_TYPE((::sign_t)) sign, const size_t& factor, default (key_sizeof<std::string, size_t>::value), const std::string& separator, default cat(":", " ") ) { // Do something... } BOOST_LOCAL_FUNCTION_NAME(f) //] std::map<std::string, size_t> m; ::sign_t sign = -1; f(m, sign); return 0; }
int main(void) { std::vector<employee> employees; employees.push_back(employee(85000)); employees.push_back(employee(100000)); employees.push_back(employee(120000)); int min_salary = 100000; int u_limit = min_salary + 1; bool BOOST_LOCAL_FUNCTION(const bind& min_salary, const bind& u_limit, const employee& e) { return e.salary >= min_salary && e.salary < u_limit; } BOOST_LOCAL_FUNCTION_NAME(between) // Pass local function to an STL algorithm as a template paramter (this // cannot be done with plain member functions of local classes). std::vector<employee>::iterator i = std::find_if( employees.begin(), employees.end(), between); BOOST_TEST(i != employees.end()); BOOST_TEST(i->salary >= min_salary && i->salary < u_limit); return boost::report_errors(); }