purpose.qbk 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. [/
  2. / Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
  3. / Copyright (c) 2003-2005 Peter Dimov
  4. /
  5. / Distributed under the Boost Software License, Version 1.0. (See
  6. / accompanying file LICENSE_1_0.txt or copy at
  7. / http://www.boost.org/LICENSE_1_0.txt)
  8. /]
  9. [section:purpose Purpose]
  10. `boost::mem_fn` is a generalization of the standard functions `std::mem_fun`
  11. and `std::mem_fun_ref`. It supports member function pointers with more than
  12. one argument, and the returned function object can take a pointer, a
  13. reference, or a smart pointer to an object instance as its first argument.
  14. `mem_fn` also supports pointers to data members by treating them as functions
  15. taking no arguments and returning a (const) reference to the member.
  16. The purpose of `mem_fn` is twofold. First, it allows users to invoke a member
  17. function on a container with the familiar
  18. std::for_each(v.begin(), v.end(), boost::mem_fn(&Shape::draw));
  19. syntax, even when the container stores smart pointers.
  20. Second, it can be used as a building block by library developers that want to
  21. treat a pointer to member function as a function object. A library might
  22. define an enhanced `for_each` algorithm with an overload of the form:
  23. template<class It, class R, class T> void for_each(It first, It last, R (T::*pmf) ())
  24. {
  25. std::for_each(first, last, boost::mem_fn(pmf));
  26. }
  27. that will allow the convenient syntax:
  28. for_each(v.begin(), v.end(), &Shape::draw);
  29. When documenting the feature, the library author will simply state:
  30. template<class It, class R, class T> void for_each(It first, It last, R (T::*pmf) ());
  31. * /Effects:/ Equivalent to `std::for_each(first, last, boost::mem_fn(pmf))`.
  32. where `boost::mem_fn` can be a link to this page. See the
  33. [@boost:/libs/bind/bind.html documentation of `bind`] for an example.
  34. `mem_fn` takes one argument, a pointer to a member, and returns a function
  35. object suitable for use with standard or user-defined algorithms:
  36. struct X
  37. {
  38. void f();
  39. };
  40. void g(std::vector<X> & v)
  41. {
  42. std::for_each(v.begin(), v.end(), boost::mem_fn(&X::f));
  43. };
  44. void h(std::vector<X *> const & v)
  45. {
  46. std::for_each(v.begin(), v.end(), boost::mem_fn(&X::f));
  47. };
  48. void k(std::vector<boost::shared_ptr<X> > const & v)
  49. {
  50. std::for_each(v.begin(), v.end(), boost::mem_fn(&X::f));
  51. };
  52. The returned function object takes the same arguments as the input member
  53. function plus a "flexible" first argument that represents the object instance.
  54. When the function object is invoked with a first argument `x` that is neither
  55. a pointer nor a reference to the appropriate class (`X` in the example above),
  56. it uses `get_pointer(x)` to obtain a pointer from `x`. Library authors can
  57. "register" their smart pointer classes by supplying an appropriate
  58. `get_pointer` overload, allowing `mem_fn` to recognize and support them.
  59. /[Note:/ `get_pointer` is not restricted to return a pointer. Any object that
  60. can be used in a member function call expression `(x->*pmf)(...)` will work./]/
  61. /[Note:/ the library uses an unqualified call to `get_pointer`. Therefore, it
  62. will find, through argument-dependent lookup, `get_pointer` overloads that are
  63. defined in the same namespace as the corresponding smart pointer class, in
  64. addition to any `boost::get_pointer` overloads./]/
  65. All function objects returned by `mem_fn` expose a `result_type` typedef that
  66. represents the return type of the member function. For data members,
  67. `result_type` is defined as the type of the member.
  68. [endsect]