Introduction
This is a brief discussion of C++ 'Pointers to Functions' and 'Pointers to Member Functions', followed by an example of building a functor template to associate an instance of a class and a pointer to a member function of that class into a transferable object. This can be used as a predicate by STL algorithms or other generic code.
Background
A question was posted on the CodeProject Visual C++ forum that led me to researching pointers to members. Subsequently, I've tidied the code snippet I presented in that thread into a more generic form and considered some more typical uses.
Pointers to Member Functions
A pointer to a plain old function is declared (for example) thus,
bool (*pfn)( int )
pfn
is a pointer to a function taking a single integer argument and returning a bool
. An example usage is:
bool f1 ( int i )
{
return i > 10 ;
}
int main ()
{
bool (*pfn)(int) ;
pfn = f1 ;
if ( (*pfn)( 11 ))
std::cout << "pfn ( 11 ) true\n" ;
return 0 ;
}
The value of pfn
becomes an actual function address as understood by the CPU.
A pointer to a member function is a slightly different animal,
bool (Foo::*pmfn)(int) ;
pmfn
is a pointer to a member function of class Foo
that takes a single integer argument and returns a bool
.
class Foo
{
private :
int t_ ;
public :
Foo ( int t ) : t_ ( t )
{
}
bool f1 ( int i )
{
return i > t_ ;
}
} ;
int main ()
{
bool (Foo::*pmfn)(int) ;
pmfn = &Foo::f1 ;
Foo foo ( 10 ) ;
if ( (foo.*pmfn)( 11 ))
std::cout << "pmfn ( 11 ) true\n" ;
return 0 ;
}
This is obviously a pointless piece of code but it suffices to show the syntax. pmfn
can be set to any member function of Foo
that matches the signature of an int
argument and bool
return. The actual implementation of pointers to members depends on whether the class has virtual functions, in which case the pointer must be a combination of the address of the relevant table of virtual function pointers and an offset into it. For a plain old class, the pointer is probably a conventional function pointer. The details are implementation and architecture dependent.
Functors
A functor is an instance of a class
or struct
that can be treated syntactically as a function.
struct PlusFunctor
{
int operator()( int l, int r )
{
return l + r ;
}
} ;
int main ()
{
PlusFunctor f ;
std::cout << "f ( 3, 4 ) = " << f ( 3, 4 ) << std::endl ;
return 0 ;
}
Function Adaptors
A function adaptor is a functor that allows functions or functors to be combined, or to have arguments bound. One interesting class of function adaptors provided by the STL allows member functions of classes to be called:
class SomeObject
{
private:
int value_ ;
public :
SomeObject () : value_ ( 0 )
{
}
SomeObject ( int value ) : value_ ( value )
{
}
void ShowValue ()
{
std::cout << value_ << " " ;
}
} ;
int main ()
{
std::vector<SomeObject> v ;
v.push_back ( SomeObject ( 1 )) ;
v.push_back ( SomeObject ( 2 )) ;
v.push_back ( SomeObject ( 3 )) ;
v.push_back ( SomeObject ( 4 )) ;
std::for_each ( v.begin (), v.end (),
std::mem_fun_ref ( &SomeObject::ShowValue )) ;
return 0 ;
}
This is calling the nominated member function for each object in the collection in a reasonably readable and concise manner. But what if we wanted to call a member function of some other object for each object in a collection? A scenario might be applying a list of updates to a document:
class Update
{
private :
public :
Update () {}
} ;
class Document
{
private :
public :
Document () {} ;
void ApplyUpdate ( const Update& update )
{
}
} ;
There are two obvious approaches to applying the updates, hand-code a loop to enumerate the container:
template <typename Container>
void ApplyUpdatesToDocument1 ( Document& doc, Container& updates )
{
Container::iterator it = updates.begin () ;
while ( it != updates.end ())
{
doc.ApplyUpdate ( *it ) ;
++it ;
}
}
By making the type of the container a template parameter, we can choose to use a vector, a list, a stack etc. The compiler can deduce the type of the container when you make the call:
int main ()
{
std::vector<Update> vu ;
vu.resize ( 10 ) ;
Document doc ;
ApplyUpdatesToDocument1 ( doc, vu ) ;
return 0 ;
}
However, it seems inelegant to have to write our own loop when for_each
is just sitting there waiting to help. One way to bring for_each
into action is to create a custom functor that keeps a reference to the document:
struct ApplyUpdateFunctor
{
Document& doc_ ;
ApplyUpdateFunctor ( Document& doc ) : doc_ ( doc )
{
}
void operator () ( const Update& update )
{
doc_.ApplyUpdate ( update ) ;
}
} ;
template < typename Container>
void ApplyUpdatesToDocument2 ( Document& doc, Container& updates )
{
std::for_each ( updates.begin (),
updates.end (), ApplyUpdateFunctor ( doc )) ;
}
This is great except that we have to write a functor for each type of operation, store them somewhere, and remember what they do when we come back to the code in a week, month or year.
The Meat of the Biscuit
With some template magic, it's possible to write a generic functor used like this:
template <typename Container>
void ApplyUpdatesToDocument3 ( Document& doc, Container& updates )
{
std::for_each ( updates.begin (), updates.end (),
mem_fun_bind1 ( doc, &Document::ApplyUpdate )) ;
}
The call looks more complex than ApplyUpdatesToDocument2
but it requires no custom functor to be defined elsewhere, and the action is written directly in the for_each
call. We want to call doc.ApplyUpdate()
for each update. Later, maintenance should hopefully require less trawling through other files.
Here is the generic functor code used above:
template <typename C, typename Arg, typename Result>
struct mem_fun_bind1_t : public std::unary_function<Arg, Result>
{
Result (C::*pmf_ )( Arg ) ;
C& rC_ ;
explicit mem_fun_bind1_t ( C& rC,
Result (C::*pmf)( Arg )) : rC_ ( rC ), pmf_ ( pmf )
{
}
Result operator () ( Arg a )
{
return (rC_.*pmf_) ( a ) ;
}
} ;
template <typename C, typename Arg,
typename Result> mem_fun_bind1_t<C, Arg, Result>
mem_fun_bind1 ( C& c, Result (C::*fn)( Arg ))
{
return mem_fun_bind1_t <C, Arg, Result>( c, fn ) ;
}
The helper function mem_fun_bind1
generates the functor mem_fun_bind1_t
. This allows the compiler to deduce the template parameters and simplifies what has to be written by hand. mem_fun_bind1_t
is directly analogous to the ApplyUpdateFunctor
shown above with the addition of a pointer to the appropriate member function and conversion to a template to allow reuse with different objects and members.
An equivalent functor for binary functions, with two arguments:
template <typename C, typename Arg1, typename Arg2, typename Result>
struct mem_fun_bind2_t : public std::binary_function<Arg1, Arg2, Result>
{
Result (C::*pmf_ )( Arg1, Arg2 ) ;
C& rC_ ;
explicit mem_fun_bind2_t ( C& rC,
Result (C::*pmf)( Arg1, Arg2 )) : rC_ ( rC ), pmf_ ( pmf )
{
}
Result operator () ( Arg1 a1, Arg2 a2 )
{
return (rC_.*pmf_) ( a1, a2 ) ;
}
} ;
template <typename C, typename Arg1, typename Arg2, typename Result>
mem_fun_bind2_t<C, Arg1, Arg2, Result>
mem_fun_bind2 ( C& c, Result (C::*fn)( Arg1, Arg2 ))
{
return mem_fun_bind2_t <C, Arg1, Arg2, Result>( c, fn ) ;
}
This binary version would suit the poster of the original std::sort
question. Extensions to more (or fewer) arguments, if you can find a use for them, is left as an exercise for the reader.
Build Notes
All code in this article has been built and tested using Visual C++ Version 7.1. This obviously requires the addition of the appropriate include files for iostream
, vector
, algorithm
and functional
.
There are no files to download, I leave the choice of name for a header file containing the functors and helper functions up to you.
Afterword
This is the first time I've come face to face with member function pointers. Investigating them, writing and testing the code snippets for this article have been educational. I'll be using
mem_fun_bind
in my work in the future. I hope this article proves of some interest even to those who find templates and the STL offensive to the eye.
History
Written and posted - May 19-20, 2004.