libsigc++: Functors

libsigc++ 2.12.2

Modules | Classes | Macros | Functions
Functors

Functors are copyable types that define operator()(). More...

## Modules
| Slots
| Slots are type-safe representations of callback methods and functions.
| mem_fun()
| mem_fun() Creates a functor from a pointer to a method.
| ptr_fun()
| ptr_fun() is used to convert a pointer to a function to a functor.

## Classes
class | sigc::can_deduce_result_type_with_decltype< T_functor >
| Helper class, to determine if decltype() can deduce the result type of a functor. More...
struct | sigc::functor_base
| A hint to the compiler. More...
struct | sigc::functor_trait< T_functor, I_derives_functor_base, I_can_use_decltype >
| Trait that specifies the return type of any type. More...
struct | sigc::visitor< T_functor >
| sigc::visitor<T_functor>::do_visit_each() performs a functor on each of the targets of a functor. More...

## Macros
#define | SIGC_FUNCTOR_TRAIT(T_functor, T_return)
| Helper macro, if you want to mix user-defined and third party functors with libsigc++.
#define | SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE
| Helper macro, if you want to mix user-defined and third party functors with libsigc++.
#define | SIGC_FUNCTORS_HAVE_RESULT_TYPE
| Helper macro, if you want to mix user-defined and third party functors with libsigc++.

## Functions
template<class T_action , class T_functor >
void | sigc::visit_each (const T_action & _A_action, const T_functor & _A_functor)
| This function performs a functor on each of the targets of a functor.
template<class T_type , class T_action , class T_functor >
void | sigc::visit_each_type (const T_action & _A_action, const T_functor & _A_functor)
| This function performs a functor on each of the targets of a functor limited to a restricted type.

## Detailed Description

Functors are copyable types that define operator()().

Types that define operator()() overloads with different return types are referred to as multi-type functors. Multi-type functors are only partially supported in libsigc++.

Closures are functors that store all information needed to invoke a callback from operator()().

Adaptors are functors that alter the signature of a functor's operator()().

libsigc++ defines numerous functors, closures and adaptors. Since libsigc++ is a callback library, most functors are also closures. The documentation doesn't distinguish between functors and closures.

The basic functor types libsigc++ provides are created with ptr_fun() and mem_fun() and can be converted into slots implicitly. The set of adaptors that ships with libsigc++ is documented in the Adaptors module.

If you want to mix user-defined and third party functors with libsigc++, and you want them to be implicitly convertible into slots, libsigc++ must know the result type of your functors. There are different ways to achieve that.

- Derive your functors from sigc::functor_base and place `typedef T_return result_type;` in the class definition.
- Use the macro SIGC_FUNCTOR_TRAIT(T_functor,T_return) in namespace sigc. Multi-type functors are only partly supported.
- For functors not derived from sigc::functor_base, and not specified with SIGC_FUNCTOR_TRAIT(), libsigc++ tries to deduce the result type with the C++11 decltype() specifier. That attempt usually succeeds if the functor has a single operator()(), but it fails if operator()() is overloaded.
- Use the macro SIGC_FUNCTORS_HAVE_RESULT_TYPE, if you want libsigc++ to assume that result_type is defined in all user-defined or third party functors, whose result type can't be deduced in any other way.

If all these ways to deduce the result type fail, void is assumed.

With libsigc++ versions before 2.6, the macro SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE activated the test with decltype(). That macro is now unneccesary and deprecated.

## Macro Definition Documentation

## ◆ SIGC_FUNCTOR_TRAIT

#define SIGC_FUNCTOR_TRAIT | ( |  | T_functor,
|  |  | T_return
| ) |  |

Helper macro, if you want to mix user-defined and third party functors with libsigc++.

If you want to mix functors not derived from sigc::functor_base with libsigc++, and these functors don't define `result_type`, use this macro inside namespace sigc to expose the return type of the functors like so:

namespace sigc {
SIGC_FUNCTOR_TRAIT(first_functor_type, return_type_of_first_functor_type)
SIGC_FUNCTOR_TRAIT(second_functor_type, return_type_of_second_functor_type)
...
}
SIGC_FUNCTOR_TRAIT
#define SIGC_FUNCTOR_TRAIT(T_functor, T_return)
Helper macro, if you want to mix user-defined and third party functors with libsigc++.
Definition functor_trait.h:188
sigc
The libsigc++ namespace.
Definition limit_reference.h:12

## ◆ SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE

#define SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE

Helper macro, if you want to mix user-defined and third party functors with libsigc++.

If you want to mix functors not derived from sigc::functor_base with libsigc++, and your compiler can deduce the result type of the functor with the C++11 keyword `decltype`, use this macro inside namespace sigc like so:

namespace sigc {
SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE
}
SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE
#define SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE
Helper macro, if you want to mix user-defined and third party functors with libsigc++.
Definition functor_trait.h:223

Functors with overloaded operator()() are not supported.

Since libsigc++ 2.2.11:

Deprecated:
  This macro does nothing. The test it activated in libsigc++ versions before 2.6, is now unconditionally activated.

## ◆ SIGC_FUNCTORS_HAVE_RESULT_TYPE

#define SIGC_FUNCTORS_HAVE_RESULT_TYPE

Helper macro, if you want to mix user-defined and third party functors with libsigc++.

If you want to mix functors not derived from sigc::functor_base with libsigc++, and these functors define `result_type`, use this macro inside namespace sigc like so:

namespace sigc { SIGC_FUNCTORS_HAVE_RESULT_TYPE }
SIGC_FUNCTORS_HAVE_RESULT_TYPE
#define SIGC_FUNCTORS_HAVE_RESULT_TYPE
Helper macro, if you want to mix user-defined and third party functors with libsigc++.
Definition functor_trait.h:165

## Function Documentation

## ◆ visit_each()

template <class T_action , class T_functor >

void sigc::visit_each | ( | const T_action & | _A_action,
|  | const T_functor & | _A_functor
| ) |  |

This function performs a functor on each of the targets of a functor.

## ◆ visit_each_type()

template <class T_type , class T_action , class T_functor >

void sigc::visit_each_type | ( | const T_action & | _A_action,
|  | const T_functor & | _A_functor
| ) |  |

This function performs a functor on each of the targets of a functor limited to a restricted type.