bind_as_compose.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <boost/config.hpp>
  2. #if defined(BOOST_MSVC)
  3. #pragma warning(disable: 4786) // identifier truncated in debug info
  4. #pragma warning(disable: 4710) // function not inlined
  5. #pragma warning(disable: 4711) // function selected for automatic inline expansion
  6. #pragma warning(disable: 4514) // unreferenced inline removed
  7. #endif
  8. //
  9. // bind_as_compose.cpp - function composition using bind.hpp
  10. //
  11. // Version 1.00.0001 (2001-08-30)
  12. //
  13. // Copyright (c) 2001 Peter Dimov and Multi Media Ltd.
  14. //
  15. // Distributed under the Boost Software License, Version 1.0. (See
  16. // accompanying file LICENSE_1_0.txt or copy at
  17. // http://www.boost.org/LICENSE_1_0.txt)
  18. //
  19. #include <boost/bind.hpp>
  20. #include <iostream>
  21. #include <string>
  22. std::string f(std::string const & x)
  23. {
  24. return "f(" + x + ")";
  25. }
  26. std::string g(std::string const & x)
  27. {
  28. return "g(" + x + ")";
  29. }
  30. std::string h(std::string const & x, std::string const & y)
  31. {
  32. return "h(" + x + ", " + y + ")";
  33. }
  34. std::string k()
  35. {
  36. return "k()";
  37. }
  38. template<class F> void test(F f)
  39. {
  40. std::cout << f("x", "y") << '\n';
  41. }
  42. int main()
  43. {
  44. using namespace boost;
  45. // compose_f_gx
  46. test( bind(f, bind(g, _1)) );
  47. // compose_f_hxy
  48. test( bind(f, bind(h, _1, _2)) );
  49. // compose_h_fx_gx
  50. test( bind(h, bind(f, _1), bind(g, _1)) );
  51. // compose_h_fx_gy
  52. test( bind(h, bind(f, _1), bind(g, _2)) );
  53. // compose_f_k
  54. test( bind(f, bind(k)) );
  55. return 0;
  56. }