libsigc++  3.6.0
Classes | Functions
bind(), bind_return()

sigc::bind() alters an arbitrary functor by fixing arguments to certain values. More...

Classes

struct  sigc::bind_functor< I_location, T_functor, T_bound >
 Adaptor that binds arguments to the wrapped functor. More...
 
struct  sigc::bind_functor<-1, T_functor, T_type... >
 Adaptor that binds argument(s) to the wrapped functor. More...
 
struct  sigc::bind_return_functor< T_return, T_functor >
 Adaptor that fixes the return value of the wrapped functor. More...
 

Functions

template<int I_location, typename T_functor , typename... T_bound>
decltype(auto) sigc::bind (const T_functor & func, T_bound... b)
 Creates an adaptor of type sigc::bind_functor which binds the passed argument to the passed functor. More...
 
template<typename T_functor , typename... T_type>
decltype(auto) sigc::bind (const T_functor & func, T_type... b)
 Creates an adaptor of type sigc::bind_functor which fixes the last arguments of the passed functor. More...
 
template<typename T_return , typename T_functor >
bind_return_functor< T_return, T_functor > sigc::bind_return (const T_functor & functor, T_return ret_value)
 Creates an adaptor of type sigc::bind_return_functor which fixes the return value of the passed functor to the passed argument. More...
 

Detailed Description

sigc::bind() alters an arbitrary functor by fixing arguments to certain values.

For single argument binding, overloads of sigc::bind() are provided that let you specify the zero-based position of the argument to fix with the first template parameter. (A value of -1 fixes the last argument so sigc::bind<-1>() gives the same result as sigc::bind().) The types of the arguments can optionally be specified if not deduced.

Examples:
void foo(int, int, int);
// single argument binding ...
sigc::bind(&foo,1)(2,3); //fixes the last (third) argument and calls foo(2,3,1)
sigc::bind<-1>(&foo,1)(2,3); //same as bind(&foo,1)(2,3) (calls foo(2,3,1))
sigc::bind<0>(&foo,1)(2,3); //fixes the first argument and calls foo(1,2,3)
sigc::bind<1>(&foo,1)(2,3); //fixes the second argument and calls foo(2,1,3)
sigc::bind<2>(&foo,1)(2,3); //fixes the third argument and calls foo(2,3,1)
// multi argument binding ...
sigc::bind(&foo,1,2)(3); //fixes the last two arguments and calls foo(3,1,2)
sigc::bind(&foo,1,2,3)(); //fixes all three arguments and calls foo(1,2,3)
decltype(auto) bind(const T_functor &func, T_bound... b)
Creates an adaptor of type sigc::bind_functor which binds the passed argument to the passed functor.
Definition: bind.h:266

The functor that sigc::bind() returns can be passed directly into sigc::signal::connect() or sigc::signal::connect_first().

Example:
sigc::signal<void()> some_signal;
void foo(int);
some_signal.connect(sigc::bind(&foo,1));

sigc::bind_return() alters an arbitrary functor by fixing its return value to a certain value.

Example:
void foo();
std::cout << sigc::bind_return(&foo, 5)(); // calls foo() and returns 5
ostream cout
bind_return_functor< T_return, T_functor > bind_return(const T_functor &functor, T_return ret_value)
Creates an adaptor of type sigc::bind_return_functor which fixes the return value of the passed funct...
Definition: bind_return.h:109

You can bind references to functors by passing the objects through the std::ref() helper function.

Example:
int some_int;
sigc::signal<void()> some_signal;
void foo(int&);
some_signal.connect(sigc::bind(&foo, std::ref(some_int)));

If you bind an object of a sigc::trackable derived type to a functor by reference, a slot assigned to the bind adaptor is cleared automatically when the object goes out of scope.

Example:
struct bar : public sigc::trackable {} some_bar;
sigc::signal<void()> some_signal;
void foo(bar&);
some_signal.connect(sigc::bind(&foo, std::ref(some_bar)));
// disconnected automatically if some_bar goes out of scope
Base class for objects with auto-disconnection.
Definition: trackable.h:118

Function Documentation

◆ bind() [1/2]

template <int I_location, typename T_functor , typename... T_bound>
decltype(auto) sigc::bind ( const T_functor &  func,
T_bound...  b 
)
inline

Creates an adaptor of type sigc::bind_functor which binds the passed argument to the passed functor.

The optional template argument I_location specifies the zero-based position of the argument to be fixed (-1 stands for the last argument).

Parameters
funcFunctor that should be wrapped.
bArguments to bind to func.
Returns
Adaptor that executes func with the bound argument on invocation.

◆ bind() [2/2]

template <typename T_functor , typename... T_type>
decltype(auto) sigc::bind ( const T_functor &  func,
T_type...  b 
)
inline

Creates an adaptor of type sigc::bind_functor which fixes the last arguments of the passed functor.

This function overload fixes the last arguments of func.

Parameters
funcFunctor that should be wrapped.
bArguments to bind to func.
Returns
Adaptor that executes func with the bound argument on invocation.

◆ bind_return()

template <typename T_return , typename T_functor >
bind_return_functor<T_return, T_functor> sigc::bind_return ( const T_functor &  functor,
T_return  ret_value 
)
inline

Creates an adaptor of type sigc::bind_return_functor which fixes the return value of the passed functor to the passed argument.

Parameters
functorFunctor that should be wrapped.
ret_valueArgument to fix the return value of functor to.
Returns
Adaptor that executes functor on invocation and returns ret_value.