[ function ]

The class template function creates polymorphic function objects. The template argument of function<> describes return and arguments types of the callable object hold (target).


A function can be constructed by means of different constructors:

function::function();                               // empty function object
function::function(null_ptr_type npc);              // empty function object

function::function(const function & rhs);           // copy constructor

template <typename Fty>
function::function(Fty fn);                         // any callable object

template <typename Fty>
function::function(reference_wrapper<Fty> fnref);   // reference_wrapper of a callable object


The member operator convertible-to-bool is provided to determine whether the function is callable or empty:

#include <tr1/functional>

function::operator convertible-to-bool() const;

int next(int a)
{ return a+1; }

std::tr1::function<int(int)> fun;

int a = 0;

if (!fun) 
fun = next;
    int b = fun(a);

operator= member function:

function& function::operator=(null_ptr_type npc);            // empty function object
function& function::operator=(const function & rhs);                    

template <typename Fty>
function& function::operator=(Fty rhs);                      // any callable object

template <typename Fty>
function& function::operator=(reference_wrapper<Fty> fnref); // reference_wrapper of a callable object

swap member function:

it swap *this and rhs callable object and does not throw exceptions:

void function::swap(function &rhs);


template <typename Fty>
bool operator==(const function<Fty> &func, null_ptr_type npc);

template <typename Fty>
bool operator==(null_ptr_type npc, const function<Fty> &func);

template <typename Fty>
bool operator!=(const function<Fty> &func, null_ptr_type npc);

template <typename Fty>
bool operator!=(null_ptr_type npc, const function<Fty> &func);

Nested types:

The function class provides a nested result_type which is the Ret type of function<Ret(T1, T2.. Tn)>: 

typedef Ret function::result_type;

If the function type takes one argument, the function is derived from std::unary_function<T1,Ret> and defines:

typedef T1 function::argument_type;

If the function type takes two arguments, the function is derived from std::binary_function<T1,T2, Ret> and defines:

typedef T1 function::first_argument_type;
typedef T2 function::second_argument_type;

Invocation and target object:

operator() makes the function object callable:

result_type function::operator()(T1 t1, T2 t2, ..., Tn tn) const;

the target_type method returns a reference to the type_info object of the target object:

const type_info & function::target_type() const;

the target method return a pointer to the target object if the target object type is Fty; it returns NULL otherwise:

template <typename Fty> Fty *function::target();
template <typename Fty> const Fty *function::target() const;