Aracılığıyla paylaş


function::function

Boş ya da sabit bir imza ile rasgele tür çağrılabilir bir nesne saklayan bir sarmalayıcı oluşturur.

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
    );

Parametreler

  • _Right
    Kopyalama işlevini nesnesi.

  • FX
    Callable nesnenin türü.

  • _Func
    Sarma callable nesnesi.

  • Ayırma
    Ayırıcı tipi.

  • _Ax
    Ayırıcısı.

  • _Fnref
    Kaydırmak için çağrılabilir nesne başvurusu.

Notlar

İlk iki Kurucular boş bir yapısı function nesnesi.Sonraki üç Kurucular değiştirmezler bir function çağrılabilir nesneyi tutan nesne işlenen geçirilen.Son iki Kurucular ayırıcısı ile depolama nesnesi _Ax ayırır.

Örnek

 

// 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; 
}
  
  

Gereksinimler

Başlık: <functional>

Namespace: std

Ayrıca bkz.

Başvuru

function Class

function::operator=

Lvalues ve Rvalues