overload_seq.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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/functional/overloaded_function.hpp>
  8. #include <boost/typeof/std/string.hpp>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <string>
  11. int add_i(int x, int y) { return x + y; }
  12. int main(void) {
  13. std::string s = "abc";
  14. std::string BOOST_LOCAL_FUNCTION(
  15. (const bind& s) (const std::string& x) ) {
  16. return s + x;
  17. } BOOST_LOCAL_FUNCTION_NAME(add_s)
  18. double d = 1.23;
  19. double BOOST_LOCAL_FUNCTION( (const bind d) (double x)
  20. (double y)(default 0) ) {
  21. return d + x + y;
  22. } BOOST_LOCAL_FUNCTION_NAME(add_d)
  23. boost::overloaded_function<
  24. std::string (const std::string&)
  25. , double (double)
  26. , double (double, double)
  27. , int (int, int)
  28. > add(add_s, add_d, add_d, add_i);
  29. BOOST_TEST(add("xyz") == "abcxyz");
  30. BOOST_TEST((4.44 - add(3.21)) <= 0.001); // Equal within precision.
  31. BOOST_TEST((44.44 - add(3.21, 40.0)) <= 0.001); // Equal within precision.
  32. BOOST_TEST(add(1, 2) == 3);
  33. return boost::report_errors();
  34. }