c++-gtk-utils: mem_fun.h File Reference

c++-gtk-utils

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

- File List
- File Members

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 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));
Cgu::MemFun::make
Functor1< Ret, Arg, T > make(T &t, Ret(T::*func)(Arg))
Definition: mem_fun.h:161
Cgu
Definition: application.h:44