blob: 36a39996cd8428a3dbf1546107f3888eab9ca4b4 [file] [log] [blame]
<HTML>
<HEAD>
<TITLE>mem_fun, mem_fun_ref</TITLE>
<LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Apache stdcxx Stylesheet"></HEAD>
<BODY BGCOLOR=#FFFFFF>
<A HREF="max-element.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="memory-h.html"><IMG SRC="images/bnext.gif" WIDTH=25 HEIGHT=21 ALT="Next file" BORDER=O></A><DIV CLASS="DOCUMENTNAME"><B>Apache C++ Standard Library Reference Guide</B></DIV>
<H2>mem_fun, mem_fun_ref</H2>
<P><B>Library:</B>&nbsp;&nbsp;<A HREF="2-4.html">General utilities</A></P>
<PRE><HR><B><I>Does not inherit</I></B><HR></PRE>
<UL>
<LI><A HREF="#sec1">Local Index</A></LI>
<LI><A HREF="#sec2">Summary</A></LI>
<LI><A HREF="#sec3">Synopsis</A></LI>
<LI><A HREF="#sec4">Description</A></LI>
<LI><A HREF="#sec5">Example</A></LI>
<LI><A HREF="#sec6">See Also</A></LI>
<LI><A HREF="#sec7">Standards Conformance</A></LI>
</UL>
<A NAME="sec1"><H3>Local Index</H3></A>
No Entries
<A NAME="sec2"><H3>Summary</H3></A>
<P>Function objects that adapt a pointer to a member function, to take the place of a global function</P>
<A NAME="sec3"><H3>Synopsis</H3></A>
<PRE>#include &lt;functional&gt;
namespace std {
template &lt;class S, class T&gt; class mem_fun_t;
template &lt;class S, class T, class A&gt; class mem_fun1_t;
template &lt;class S, class T&gt; class mem_fun_ref_t;
template &lt;class S, class T, class A&gt; class mem_fun1_ref_t;
template &lt;class S, class T&gt; class const_mem_fun_t;
template &lt;class S, class T, class A&gt; class const_mem_fun1_t;
template &lt;class S, class T&gt; class const_mem_fun_ref_t;
template &lt;class S, class T, class A&gt;
class const_mem_fun1_ref_t;
template&lt;class S, class T&gt; mem_fun_t&lt;S,T&gt;
mem_fun(S, (T::*f)());
template&lt;class S, class T&gt; mem_fun_ref_t&lt;S,T&gt;
mem_fun_ref(S, (T::*f)());
template&lt;class S, class T. class A&gt; mem_fun1_t&lt;S,T,A&gt;
mem_fun(S, (T::*f)(A));
template&lt;class S, class T, class A&gt; mem_fun1_ref_t&lt;S,T,A&gt;
mem_fun_ref(S, (T::*f)(A));
template&lt;class S, class T&gt; const_mem_fun_t&lt;S,T&gt;
mem_fun(S, (T::*f)() const);
template&lt;class S, class T&gt; const_mem_fun_ref_t&lt;S,T&gt;
mem_fun_ref(S, (T::*f)() const);
template&lt;class S, class T, class A&gt; const_mem_fun1_t&lt;S,T,A&gt;
mem_fun(S, (T::*f)(A) const);
template&lt;class S, class T, class A&gt;
const_mem_fun1_ref_t&lt;S,T,A&gt;
mem_fun_ref(S, (T::*f)(A) const);
}
</PRE>
<A NAME="sec4"><H3>Description</H3></A>
<P>The <B><I><A HREF="mem-fun.html">mem_fun</A></I></B> group of templates encapsulates a pointer to a member function. Each category of template (<B><I>mem_fun</I></B> or <B><I><A HREF="mem-fun.html">mem_fun_ref</A></I></B>) includes both a class template and a function template, where the class is distinguished by the addition of <SAMP>_t</SAMP> on the end of the name to identify it as a type. A set of class templates for const member functions exists, each with <SAMP>const_</SAMP> prepended to the name.</P>
<P>The class's constructor takes a pointer to a member function and uses <SAMP>operator()</SAMP> to forward the call to that member function. The resulting object serves as a global function object for that member function. </P>
<P>The accompanying function template simplifies the use of this facility by constructing an instance of the class on the fly. </P>
<P>The library includes zero and one argument adaptors for containers of pointers and containers of references (<SAMP>_ref</SAMP>). This technique can be easily extended to include adaptors for two argument functions, and so on. </P>
<A NAME="sec5"><H3>Example</H3></A>
<UL><PRE>//
// mem_fun example
//
#include &lt;functional&gt;
#include &lt;list&gt;
using namespace std;
int main(void)
{
int a1[] = {2,1,5,6,4};
int a2[] = {11,4,67,3,14};
list&lt;int&gt; s1(a1,a1+5);
list&lt;int&gt; s2(a2,a2+5);
// Build a list of lists
list&lt;list&lt;int&gt;* &gt; l;
l.insert(l.begin(),s1);
l.insert(l.begin(),s2);
// Sort each list in the list
for_each(l.begin(),l.end(),mem_fun(&amp;list&lt;int&gt;::sort));
}
</PRE></UL>
<A NAME="sec6"><H3>See Also</H3></A>
<P><B><I><A HREF="binary-function.html">binary_function</A></I></B>, <A HREF="functionobjects.html">Function Objects</A>, <B><I><A HREF="pointer-to-unary-function.html">pointer_to_unary_function</A></I></B>, <SAMP><A HREF="ptr-fun.html">ptr_fun()</A></SAMP></P>
<A NAME="sec7"><H3>Standards Conformance</H3></A>
<P><I>ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 20.3.8</I></P>
<BR>
<HR>
<A HREF="max-element.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="memory-h.html"><IMG SRC="images/bnext.gif" WIDTH=20 HEIGHT=21 ALT="Next file" BORDER=O></A>
<!-- Google Analytics tracking code -->
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-1775151-1";
urchinTracker();
</script>
<!-- end of Google Analytics tracking code -->
</BODY>
</HTML>