bind.qbk 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. [/==============================================================================
  2. Copyright (C) 2001-2010 Joel de Guzman
  3. Copyright (C) 2001-2005 Dan Marsden
  4. Copyright (C) 2001-2010 Thomas Heller
  5. Copyright (C) 2015 John Fletcher
  6. Distributed under the Boost Software License, Version 1.0. (See accompanying
  7. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. ===============================================================================/]
  9. [section Bind]
  10. ['Binding] is the act of tying together a function to some arguments for
  11. deferred (lazy) evaluation. Named [link phoenix.modules.function lazy functions]
  12. require a bit of typing. Unlike (unnamed) lambda expressions, we need to write a
  13. functor somewhere offline, detached from the call site. If you wish to transform a
  14. plain function, member function or member variable to a lambda expression, `bind`
  15. is your friend.
  16. [note Take note that binding functions, member functions or member variables is
  17. monomorphic. Rather than binding functions, the preferred way is to write true
  18. generic and polymorphic [link phoenix.modules.function lazy functions].]
  19. There is a set of overloaded `bind` template functions. Each `bind(x)`
  20. function generates a suitable binder object.
  21. [section Binding Function Objects]
  22. #include <boost/phoenix/bind/bind_function_object.hpp>
  23. Binding function objects serves two purposes:
  24. * Partial function application
  25. * Quick adaption of already existing function objects
  26. In order to deduce the return type of the function object, it has to implement
  27. the __boost_result_of__ protocol. If the bound function object is polymorphic,
  28. the resulting binding object is polymorphic.
  29. [endsect]
  30. [section Binding Functions]
  31. #include <boost/phoenix/bind/bind_function.hpp>
  32. Example, given a function `foo`:
  33. void foo(int n)
  34. {
  35. std::cout << n << std::endl;
  36. }
  37. Here's how the function `foo` may be bound:
  38. bind(&foo, arg1)
  39. This is now a full-fledged expression that can finally
  40. be evaluated by another function call invocation. A second function call will
  41. invoke the actual `foo` function. Example:
  42. bind(&foo, arg1)(4);
  43. will print out "4".
  44. [endsect]
  45. [section Binding Member Functions]
  46. #include <boost/phoenix/bind/bind_member_function.hpp>
  47. Binding member functions can be done similarly. A bound member function takes in
  48. a pointer or reference to an object as the first argument. For instance, given:
  49. struct xyz
  50. {
  51. void foo(int) const;
  52. };
  53. `xyz`'s `foo` member function can be bound as:
  54. bind(&xyz::foo, obj, arg1) // obj is an xyz object
  55. Take note that a lazy-member functions expects the first argument to be a
  56. pointer or reference to an object. Both the object (reference or pointer) and
  57. the arguments can be lazily bound. Examples:
  58. xyz obj;
  59. bind(&xyz::foo, arg1, arg2) // arg1.foo(arg2)
  60. bind(&xyz::foo, obj, arg1) // obj.foo(arg1)
  61. bind(&xyz::foo, obj, 100) // obj.foo(100)
  62. [endsect]
  63. [section Binding Member Variables]
  64. #include <boost/phoenix/bind/bind_member_variable.hpp>
  65. Member variables can also be bound much like member functions. Member variables
  66. are not functions. Yet, like the [link phoenix.modules.core.references `ref(x)`] that
  67. acts like a nullary function returning a reference to the data, member variables,
  68. when bound, act like a unary function, taking in a pointer or reference to an
  69. object as its argument and returning a reference to the bound member variable.
  70. For instance, given:
  71. struct xyz
  72. {
  73. int v;
  74. };
  75. `xyz::v` can be bound as:
  76. bind(&xyz::v, obj) // obj is an xyz object
  77. As noted, just like the bound member function, a bound member variable also
  78. expects the first (and only) argument to be a pointer or reference to an object.
  79. The object (reference or pointer) can be lazily bound. Examples:
  80. xyz obj;
  81. bind(&xyz::v, arg1) // arg1.v
  82. bind(&xyz::v, obj) // obj.v
  83. bind(&xyz::v, arg1)(obj) = 4 // obj.v = 4
  84. [endsect]
  85. [section Compatibility with Boost.Bind]
  86. `phoenix::bind` passes the older testcases of the Boost.Bind library. For those cases it is completely compatible and interchangeable. Some newer cases have been added to Boost.Bind in 2015 and compatibility in those cases is not guaranteed.
  87. Further tests are needed to check compatibility with std::tr1::bind and std::bind from the C++11 standard.
  88. [endsect]
  89. [endsect]