c++-gtk-utils
Classes | Functions
Cgu::MemFun Namespace Reference

This namespace contains an adaptor to create a functor from a class member function to pass to C++ standard algorithms. More...

Classes

class  Functor1
 
class  Functor1_const
 
class  Functor2
 
class  Functor2_const
 

Functions

template<class Ret , class Arg , class T >
Functor1< Ret, Arg, T > make (T &t, Ret(T::*func)(Arg))
 
template<class Ret , class Arg , class T >
Functor1_const< Ret, Arg, T > make (const T &t, Ret(T::*func)(Arg) const)
 
template<class Ret , class Arg1 , class Arg2 , class T >
Functor2< Ret, Arg1, Arg2, T > make (T &t, Ret(T::*func)(Arg1, Arg2))
 
template<class Ret , class Arg1 , class Arg2 , class T >
Functor2_const< Ret, Arg1, Arg2, T > make (const T &t, Ret(T::*func)(Arg1, Arg2) const)
 

Detailed Description

This namespace contains an adaptor to create a functor from a class member function to pass to C++ standard algorithms.

#include <c++-gtk-utils/mem_fun.h>

Cgu::MemFun::make() is an adaptor which allows a non-static class member function to be called by standard algorithms such as std::for_each() or std::transform(), whereby the member function is passed a contained object as an argument on iterating through the container. It can also be used to make predicates from member functions to pass to other algorithms, or indeed to generate a general functor object representing a member function with object and one or two arguments. It does the equivalent of std::ptr_fun() for ordinary functions.

For example, to iterate over a std::vector<int> container object named 'vec', calling a class method 'void MyObj::my_method(int)' on each iteration, Cgu::MemFun::make() could be invoked as:

using namespace Cgu;
std::for_each(vec.begin(), vec.end(),
MemFun::make(my_obj, &MyObj::my_method));

As in the case of std::ptr_fun(), when called via std::for_each(), the member function called must be a unary function, although an additional argument can be bound with std::bind2nd() or std::bind1st() to enable it to be used with binary class functions. (Note that in many implementations of std::bind*, neither the free nor the bound argument can be a reference, whether const or non-const. A reference argument can be employed with Cgu::MemFun::make() where std::bind* is not used.)

If (in C++98) the member function is const, then the unary Cgu::MemFun::make() can be mimicked by a combination of std::mem_fun() and std::bind1st(), so that (if const) the above example could be reproduced as:

std::for_each(vec.begin(), vec.end(),
std::bind1st(std::mem_fun(&MyObj::my_method), &my_obj));

The binary version of Cgu::MemFun::make() cannot be reproduced by any of the built-in function adaptors available in C++98/C++03.

Function Documentation

template<class Ret , class Arg , class T >
Functor1<Ret, Arg, T> Cgu::MemFun::make ( T &  t,
Ret(T::*)(Arg)  func 
)
template<class Ret , class Arg , class T >
Functor1_const<Ret, Arg, T> Cgu::MemFun::make ( const T &  t,
Ret(T::*)(Arg) const  func 
)
template<class Ret , class Arg1 , class Arg2 , class T >
Functor2<Ret, Arg1, Arg2, T> Cgu::MemFun::make ( T &  t,
Ret(T::*)(Arg1, Arg2)  func 
)
template<class Ret , class Arg1 , class Arg2 , class T >
Functor2_const<Ret, Arg1, Arg2, T> Cgu::MemFun::make ( const T &  t,
Ret(T::*)(Arg1, Arg2) const  func 
)