libsigc++: Functors

libsigc++ 2.2.11

- Main Page
- Modules
- Namespaces
- Classes

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() is used to convert a pointer to a method to a functor.
| ptr_fun()
| ptr_fun() is used to convert a pointer to a function to a functor.

## Classes
struct | sigc::functor_base
| A hint to the compiler. More...
struct | sigc::functor_trait< T_functor, I_derives_functor_base >
| Trait that specifies the return type of any type. More...

## Macros
#define | SIGC_FUNCTORS_HAVE_RESULT_TYPE
| Helper macro, if you want to mix user-defined and third party functors with libsigc++.
#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++.

## 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.
- 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, except those for which you specify a return type explicitly with SIGC_FUNCTOR_TRAIT().
- Use the macro SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE, if your compiler makes it possible. Functors with overloaded operator()() are not supported.

The last alterative makes it possible to construct a slot from a C++11 lambda expression with any return type. Example:

namespace sigc {
SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE
}
sigc::slot<bool, int> slot1 = [](int n)-> bool
{
return n == 42;
};

## Macro Definition Documentation

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

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

You can't use both SIGC_FUNCTORS_HAVE_RESULT_TYPE and SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE in the same compilation unit.

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

You can't use both SIGC_FUNCTORS_HAVE_RESULT_TYPE and SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE in the same compilation unit.

## Function Documentation

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.

All unknown types just call _A_action on them. Add overloads that specialize the T_functor argument for your own functor types, so that subobjects get visited. This is needed to enable auto-disconnection support for your functor types.

Example:
  struct some_functor
  {
  void operator()() {}
  some_possibly_sigc_trackable_derived_type some_data_member;
  some_other_functor_type some_other_functor;
  }
  namespace sigc
  {
  template <class T_action>
  void visit_each(const T_action& _A_action,
  const some_functor& _A_target)
  {
  visit_each(_A_action, _A_target.some_data_member);
  visit_each(_A_action, _A_target.some_other_functor);
  }
  }