From f17fa79f2bddf57577f1a65e4f22b19255787193 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sat, 24 Nov 2001 17:03:11 +0000 Subject: [PATCH] mem_fn.html: added Contents, FAQ, Dependencies, expanded Purpose. [SVN r11777] --- bind.html | 2 +- mem_fn.html | 141 +++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 125 insertions(+), 18 deletions(-) diff --git a/bind.html b/bind.html index ae0064a..dee6dd6 100644 --- a/bind.html +++ b/bind.html @@ -7,7 +7,7 @@ Boost: bind.hpp documentation - + diff --git a/mem_fn.html b/mem_fn.html index d921f1c..31a4e22 100644 --- a/mem_fn.html +++ b/mem_fn.html @@ -7,7 +7,7 @@ Boost: mem_fn.hpp documentation - +
@@ -23,15 +23,27 @@
-

Files

- +

Contents

-

Purpose

+

Purpose

+

Frequently Asked Questions

+

Can mem_fn be used instead of the standard +std::mem_fun[_ref] adaptors?

+

Should I replace every occurence of std::mem_fun[_ref] +with mem_fn in my existing code?

+

Interface

+

Synopsis

+

Common requirements

+

get_pointer

+

mem_fn

+

Implementation

+

Files

+

Dependencies

+

Number of Arguments

+

__stdcall Support

+

Acknowledgements

+ +

Purpose

boost::mem_fn is a generalization of the standard functions @@ -41,6 +53,55 @@ object can take a pointer, a reference, or a smart pointer to an object instance as its first argument.

+

+The purpose of mem_fn is twofold. First, it allows users to invoke a +member function on a container with the familiar +

+ +
+    std::for_each(v.begin(), v.end(), boost::mem_fn(&Shape::draw));
+
+ +

+syntax, even when the container stores smart pointers. +

+ +

+Second, it can be used as a building block by library developers that want +to treat a pointer to member function as a function object. A library might +define an enhanced for_each algorithm with an overload of the form: +

+ +
+template<class It, class R> void for_each(It first, It last, R (*pmf) ())
+{
+    std::for_each(first, last, boost::mem_fn(pmf));
+}
+
+ +

+that will allow the convenient syntax: +

+ +
+    for_each(v.begin(), v.end(), &Shape::draw);
+
+ +

+When documenting the feature, the library author will simply state: +

+ +

template<class It, class R> void for_each(It first, It last, R (*pmf) ());

+ +

+Effects: equivalent to std::for_each(first, last, boost::mem_fn(pmf)); +

+ +

+where boost::mem_fn can be a link to this page. See +the documentation of bind for an example. +

+

mem_fn takes one argument, a pointer to a member function, and returns a function object suitable for use with standard or user-defined @@ -105,9 +166,37 @@ All function objects returned by mem_fn expose a result_type typedef that represents the return type of the member function.

-

Interface

+

Frequently Asked Questions

-

Synopsis

+

Can mem_fn be used instead of the standard +std::mem_fun[_ref] adaptors?

+ +

+Yes. For simple uses, mem_fn provides additional functionality that +the standard adaptors do not. Complicated expressions that use std::bind1st, +std::bind2nd or Boost.Compose +along with the standard adaptors can be rewritten using +boost::bind that automatically takes advantage of +mem_fn. +

+ +

Should I replace every occurence of std::mem_fun[_ref] +with mem_fn in my existing code?

+ +

+No, unless you have good reasons to do so. mem_fn is not 100% compatible +with the standard adaptors, although it comes pretty close. In particular, +mem_fn does not return objects of type +std::[const_]mem_fun[1][_ref]_t, as the standard adaptors do, and it is +not possible to fully describe the type of the first argument using the standard +argument_type and first_argument_type nested typedefs. Libraries +that need adaptable function objects in order to function might not like +mem_fn. +

+ +

Interface

+ +

Synopsis

 namespace boost
@@ -134,7 +223,7 @@ template<class R, class T, class A1, class A2> implementation-defined-6
 }
 
-

Common requirements

+

Common requirements

All implementation-defined-N types mentioned in the Synopsis are @@ -145,7 +234,7 @@ the return type of the member function pointer passed as an argument to mem_f (R in the Synopsis.)

-

get_pointer

+

get_pointer

template<class T> T * get_pointer(T * p)

@@ -165,7 +254,7 @@ the return type of the member function pointer passed as an argument to mem_f Throws: Nothing.

-

mem_fn

+

mem_fn

template<class R, class T> implementation-defined-1 mem_fn(R (T::*pmf) ())

@@ -233,7 +322,25 @@ is of type T [const], (get_pointer(t)->*pmf)(a1, a2)Throws:
Nothing.

-

Implementation

+

Implementation

+ +

Files

+ + +

Dependencies

+ + +

Number of Arguments

This implementation supports member functions with up to eight arguments. @@ -241,7 +348,7 @@ This is not an inherent limitation of the design, but an implementation detail.

-

__stdcall support

+

__stdcall Support

Some platforms allow several types of member functions that differ by their @@ -270,7 +377,7 @@ indirectly, <boost/mem_fn.hpp>.

-

Acknowledgements

+

Acknowledgements

Rene Jager's initial suggestion of using traits classes to make