c++-gtk-utils
Classes | Namespaces | Functions
mem_fun.h File Reference

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

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

Go to the source code of this file.

Classes

class  Cgu::MemFun::Functor1< Ret, Arg, T >
 
class  Cgu::MemFun::Functor1_const< Ret, Arg, T >
 
class  Cgu::MemFun::Functor2< Ret, Arg1, Arg2, T >
 
class  Cgu::MemFun::Functor2_const< Ret, Arg1, Arg2, T >
 

Namespaces

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

Functions

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

Detailed Description

This file 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.

Cgu::MemFun::make
Functor1< Ret, Arg, T > make(T &t, Ret(T::*func)(Arg))
Definition: mem_fun.h:159
Cgu
Definition: application.h:45
Cgu::DoIf::mem_fun
Ret mem_fun(Obj &obj, Ret(Base::*func)(Arg1, Arg2), typename Cgu::Param< Arg1 >::ParamType arg1, typename Cgu::Param< Arg2 >::ParamType arg2, typename SucceedOnTrue< RelatedTest< Base, Obj >::value >::T t=0)
Definition: do_if.h:871