• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 // Copyright (C) 2009-2012 Lorenzo Caminiti
3 // Distributed under the Boost Software License, Version 1.0
4 // (see accompanying file LICENSE_1_0.txt or a copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 // Home at http://www.boost.org/libs/local_function
7 
8 #include <boost/local_function.hpp>
9 #include <boost/typeof/typeof.hpp>
10 #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
11 #include <boost/detail/lightweight_test.hpp>
12 #include <vector>
13 #include <algorithm>
14 
15 struct employee {
16     int salary;
employeeemployee17     explicit employee(const int& a_salary): salary(a_salary) {}
18 };
BOOST_TYPEOF_REGISTER_TYPE(employee)19 BOOST_TYPEOF_REGISTER_TYPE(employee) // Register for `NAME` below.
20 
21 int main(void) {
22     std::vector<employee> employees;
23     employees.push_back(employee(85000));
24     employees.push_back(employee(100000));
25     employees.push_back(employee(120000));
26 
27     int min_salary = 100000;
28     int u_limit = min_salary + 1;
29 
30     bool BOOST_LOCAL_FUNCTION(const bind& min_salary, const bind& u_limit,
31             const employee& e) {
32         return e.salary >= min_salary && e.salary < u_limit;
33     } BOOST_LOCAL_FUNCTION_NAME(between)
34 
35     // Pass local function to an STL algorithm as a template paramter (this
36     // cannot be done with plain member functions of local classes).
37     std::vector<employee>::iterator i = std::find_if(
38             employees.begin(), employees.end(), between);
39 
40     BOOST_TEST(i != employees.end());
41     BOOST_TEST(i->salary >= min_salary && i->salary < u_limit);
42     return boost::report_errors();
43 }
44 
45