n2550_find_if.cpp 1.5 KB

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