function::function

Construye un contenedor que está vacío o almacena un objeto accesible de tipo arbitrario con una firma fija.

function();
function(nullptr_t npc);
function(const function& _Right);
template<class Fx>
   function(Fx _Func);
template<class Fx>
    function(reference_wrapper<Fx> _Fnref);
template<class Fx, class Alloc>
    function(
        Fx _Func, 
        const Alloc& _Ax
);
template<class Fx, class Alloc>
    function(
        reference_wrapper<Fx> _Fnref, 
        const Alloc& _Ax
    );

Parámetros

  • _Right
    el objeto de la función a la copia.

  • Fx
    El tipo del objeto accesible.

  • _Func
    El objeto accesible al ajuste.

  • Alloc
    El tipo del asignador.

  • _Ax
    el asignador.

  • _Fnref
    La referencia de objeto accesible al ajuste.

Comentarios

Los primeros dos constructores crean un objeto vacío de function .Los tres constructores siguientes crean un objeto de function que contiene el objeto accesible pasado como el operando.Los dos constructores pasados asignan almacenamiento con el _Ax del asignador.

Ejemplo

 

// std_tr1__functional__function_function.cpp 
// compile with: /EHsc 
#include <functional> 
#include <iostream> 
#include <vector>
 
int square(int val)
{
    return val * val;
}

class multiply_by
{
public:
    explicit multiply_by(const int n) : m_n(n) { }

    int operator()(const int x) const
    {
        return m_n * x;
    }

private:
    int m_n;
};


int main() 
{ 

    typedef std::vector< std::function<int (int)> > vf_t;

    vf_t v;
    v.push_back(square);
    v.push_back(std::negate<int>());
    v.push_back(multiply_by(3));

    for (vf_t::const_iterator i = v.begin(); i != v.end(); ++i)
    {
        std::cout << (*i)(10) << std::endl;
    }

    std::function<int (int)> f = v[0];
    std::function<int (int)> g;

    if (f) {
        std::cout << "f is non-empty (correct)." << std::endl;
    } else {
        std::cout << "f is empty (can't happen)." << std::endl;
    }
 
    if (g) {
        std::cout << "g is non-empty (can't happen)." << std::endl;
    } else {
        std::cout << "g is empty (correct)." << std::endl;
    }

    return 0; 
}
  
  

Requisitos

encabezado: <funcional>

espacio de nombres: std

Vea también

Referencia

function Class

function::operator=

Son y valores r