c++-gtk-utils: Cgu::MemFun Namespace Reference

c++-gtk-utils

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- Namespace List
- Namespace Members

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 provided in order to provide source compatibility with the 1.2 series of the library. In C++11/14 it is superseded by std::mem_fn() and std::bind.

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::bind1st/2nd, 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::bind1st/2nd is not used.) This limitation with respect to reference arguments is not present with std::mem_fn() and std::bind.

In C++11/14, the above example could be reproduced as:

using namespace std::placeholders; // for _1
std::for_each(vec.begin(), vec.end(),
std::bind(&MyObj::my_method, &my_obj, _1));

## 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
| ) |  |