Aracılığıyla paylaş


function Sınıfı

Çağrılabilen bir nesne için sarmalayıcı.

Sözdizimi

template <class Fty>
class function  // Fty of type Ret(T1, T2, ..., TN)
    : public unary_function<T1, Ret>       // when Fty is Ret(T1)
    : public binary_function<T1, T2, Ret>  // when Fty is Ret(T1, T2)
{
public:
    typedef Ret result_type;

    function();
    function(nullptr_t);
    function(const function& right);
    template <class Fty2>
        function(Fty2 fn);
    template <class Fty2, class Alloc>
        function(reference_wrapper<Fty2>, const Alloc& Ax);

    template <class Fty2, class Alloc>
        void assign(Fty2, const Alloc& Ax);
    template <class Fty2, class Alloc>
        void assign(reference_wrapper<Fty2>, const Alloc& Ax);
    function& operator=(nullptr_t);
    function& operator=(const function&);
    template <class Fty2>
        function& operator=(Fty2);
    template <class Fty2>
        function& operator=(reference_wrapper<Fty2>);

    void swap(function&);
    explicit operator bool() const;

    result_type operator()(T1, T2, ....., TN) const;
    const std::type_info& target_type() const;
    template <class Fty2>
        Fty2 *target();

    template <class Fty2>
        const Fty2 *target() const;

    template <class Fty2>
        void operator==(const Fty2&) const = delete;
    template <class Fty2>
        void operator!=(const Fty2&) const = delete;
};

Parametreler

Fty
Kaydıracak işlev türü.

Ax
Ayırıcı işlevi.

Açıklamalar

Sınıf şablonu, çağrı imzası olan bir çağrı sarmalayıcısıdır Ret(T1, T2, ..., TN). Çeşitli çağrılabilen nesneleri tekdüzen sarmalayıcı içine almak için bunu kullanırsınız.

Bazı üye işlevleri, istenen hedef nesneyi adlandıran bir işlenen alır. Böyle bir işleneni çeşitli yollarla belirtebilirsiniz:

fn: Çağrılabilen nesne fn; çağrıdan sonra nesnenin function bir kopyasını barındırıyor: fn

fnref: tarafından adlandırılan fnref.get()çağrılabilen nesne; çağrısından sonra nesnenin başvuruyu barındırdığı functionfnref.get()

right: Varsa, nesne tarafından function tutulan çağrılabilen nesne right

npc: Null işaretçi; çağrıdan function sonra nesne boş olur

Her durumda , INVOKE(f, t1, t2, ..., tN)f çağrılabilen nesnedir ve t1, t2, ..., tN sırasıyla türlerin T1, T2, ..., TN lvalue'larıdır, iyi biçimlendirilmiş ve geçersiz değilse Ret olarak dönüştürülebilir Retolmalıdır.

Boş function bir nesne çağrılabilen bir nesneyi veya çağrılabilen bir nesneye başvuru içermez.

Üyeler

Oluşturucular

Ad Tanım
Işlev Boş olan bir sarmalayıcı oluşturur veya sabit imzayla rastgele türde çağrılabilen bir nesne depolar.

Tür tanımları

Ad Tanım
result_type Depolanan çağrılabilir nesnenin dönüş türü.

İşlevler

Ad Tanım
Atamak Bu işlev nesnesine çağrılabilen bir nesne atar.
Takas İki çağrılabilen nesneyi değiştirin.
Hedef Depolanmış çağrılabilen nesnenin belirtilen şekilde çağrılabilir olup olmadığını sınar.
target_type Çağrılabilen nesnedeki tür bilgilerini alır.

İşleçler

Ad Tanım
işleç belirtilmemiş Depolanmış çağrılabilen nesne olup olmadığını test eder.
operator() Çağrılabilen bir nesneyi çağırır.
operator= Depolanan çağrılabilir nesnenin yerini alır.

atamak

Bu işlev nesnesine çağrılabilen bir nesne atar.

template <class Fx, class Alloc>
    void assign(
        Fx _Func,
        const Alloc& Ax);

template <class Fx, class Alloc>
    void assign(
        reference_wrapper<Fx> _Fnref,
        const Alloc& Ax);

Parametreler

_Func
Çağrılabilen bir nesne.

_Fnref
Çağrılabilen bir nesne içeren başvuru sarmalayıcı.

Ax
Ayırıcı nesnesi.

Açıklamalar

Üye işlevlerinin callable object her biri tarafından *this tutulan öğesini olarak geçirilen operandçağrılabilir nesneyle değiştirir. Her ikisi de ayırıcı nesnesi Ax ile depolama ayırır.

function

Boş olan bir sarmalayıcı oluşturur veya sabit imzayla rastgele türde çağrılabilen bir nesne depolar.

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

Doğru
Kopyalanacak işlev nesnesi.

Fx
Çağrılabilen nesnenin türü.

_Func
Kaydırılabilir çağrılabilen nesne.

Ayırma
Ayırıcı türü.

Ax
Ayırıcı.

_Fnref
Sarmalama için çağrılabilen nesne başvurusu.

Açıklamalar

İlk iki oluşturucu boş function bir nesne oluşturur. Sonraki üç oluşturucu, işlenen olarak geçirilen çağrılabilir nesneyi tutan bir function nesne oluşturur. Son iki oluşturucu ayırıcı nesnesi Ax ile depolama ayırır.

Örnek

// std__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;
}
100
-10
30
f is non-empty (correct).
g is empty (correct).

işleç belirtilmemiş

Depolanmış çağrılabilen nesne olup olmadığını test eder.

operator unspecified();

Açıklamalar

işleci, yalnızca nesne boş değilse gerçek bir değerle dönüştürülebilir bool bir değer döndürür. Nesnenin boş olup olmadığını test etmek için bunu kullanırsınız.

Örnek

// std__functional__function_operator_bool.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>

int neg(int val)
    {
    return (-val);
    }

int main()
    {
    std::function<int (int)> fn0;
    std::cout << std::boolalpha << "not empty == " << (bool)fn0 << std::endl;

    std::function<int (int)> fn1(neg);
    std::cout << std::boolalpha << "not empty == " << (bool)fn1 << std::endl;

    return (0);
    }
not empty == false
not empty == true

operator()

Çağrılabilen bir nesneyi çağırır.

result_type operator()(
    T1 t1,
    T2 t2, ...,
    TN tN);

Parametreler

TN
N. çağrı bağımsız değişkeninin türü.

Tn
N. çağrı bağımsız değişkeni.

Açıklamalar

üye işlevi döndürür INVOKE(fn, t1, t2, ..., tN, Ret), burada fn içinde *thisdepolanan hedef nesnedir. Sarmalanmış çağrılabilir nesnesini çağırmak için bunu kullanırsınız.

Örnek

// std__functional__function_operator_call.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>

int neg(int val)
    {
    return (-val);
    }

int main()
    {
    std::function<int (int)> fn1(neg);
    std::cout << std::boolalpha << "empty == " << !fn1 << std::endl;
    std::cout << "val == " << fn1(3) << std::endl;

    return (0);
    }
empty == false
val == -3

operator=

Depolanan çağrılabilir nesnenin yerini alır.

function& operator=(null_ptr_type npc);
function& operator=(const function& right);
template <class Fty>
    function& operator=(Fty fn);
template <class Fty>
    function& operator=(reference_wrapper<Fty> fnref);

Parametreler

Npc
Null işaretçi sabiti.

Doğru
Kopyalanacak işlev nesnesi.

Fn
Kaydırılabilir çağrılabilen nesne.

fnref
Sarmalama için çağrılabilen nesne başvurusu.

Açıklamalar

İşleçlerin her biri tarafından *this tutulan çağrılabilir nesneyi işlenen olarak geçirilen çağrılabilir nesneyle değiştirir.

Örnek

// std__functional__function_operator_as.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>

int neg(int val)
    {
    return (-val);
    }

int main()
    {
    std::function<int (int)> fn0(neg);
    std::cout << std::boolalpha << "empty == " << !fn0 << std::endl;
    std::cout << "val == " << fn0(3) << std::endl;

    std::function<int (int)> fn1;
    fn1 = 0;
    std::cout << std::boolalpha << "empty == " << !fn1 << std::endl;

    fn1 = neg;
    std::cout << std::boolalpha << "empty == " << !fn1 << std::endl;
    std::cout << "val == " << fn1(3) << std::endl;

    fn1 = fn0;
    std::cout << std::boolalpha << "empty == " << !fn1 << std::endl;
    std::cout << "val == " << fn1(3) << std::endl;

    fn1 = std::cref(fn1);
    std::cout << std::boolalpha << "empty == " << !fn1 << std::endl;
    std::cout << "val == " << fn1(3) << std::endl;

    return (0);
    }
empty == false
val == -3
empty == true
empty == false
val == -3
empty == false
val == -3
empty == false
val == -3

result_type

Depolanan çağrılabilir nesnenin dönüş türü.

typedef Ret result_type;

Açıklamalar

typedef, şablonun çağrı imzasında türün Ret eş anlamlısıdır. Sarmalanmış çağrılabilen nesnenin dönüş türünü belirlemek için bunu kullanırsınız.

Örnek

// std__functional__function_result_type.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>

int neg(int val)
    {
    return (-val);
    }

int main()
    {
    std::function<int (int)> fn1(neg);
    std::cout << std::boolalpha << "empty == " << !fn1 << std::endl;

    std::function<int (int)>::result_type val = fn1(3);
    std::cout << "val == " << val << std::endl;

    return (0);
    }
empty == false
val == -3

swap

İki çağrılabilen nesneyi değiştirin.

void swap(function& right);

Parametreler

Doğru
Ile değiştir iletişim kurmak için işlev nesnesi.

Açıklamalar

Üye işlevi, hedef nesneleri ile sağ arasında *this değiştirir. Bunu sürekli olarak yapar ve hiçbir özel durum oluşturmaz.

Örnek

// std__functional__function_swap.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>

int neg(int val)
    {
    return (-val);
    }

int main()
    {
    std::function<int (int)> fn0(neg);
    std::cout << std::boolalpha << "empty == " << !fn0 << std::endl;
    std::cout << "val == " << fn0(3) << std::endl;

    std::function<int (int)> fn1;
    std::cout << std::boolalpha << "empty == " << !fn1 << std::endl;
    std::cout << std::endl;

    fn0.swap(fn1);
    std::cout << std::boolalpha << "empty == " << !fn0 << std::endl;
    std::cout << std::boolalpha << "empty == " << !fn1 << std::endl;
    std::cout << "val == " << fn1(3) << std::endl;

    return (0);
    }
empty == false
val == -3
empty == true

empty == true
empty == false
val == -3

hedef

Depolanmış çağrılabilen nesnenin belirtilen şekilde çağrılabilir olup olmadığını sınar.

template <class Fty2>
    Fty2 *target();
template <class Fty2>
    const Fty2 *target() const;

Parametreler

Fty2
Test etmek için hedef çağrılabilen nesne türü.

Açıklamalar

Fty2 türü, bağımsız değişken türleri ve dönüş türü T1, T2, ..., TNRetiçin çağrılabilir olmalıdır. ise target_type() == typeid(Fty2), üye şablonu işlevi hedef nesnenin adresini döndürür; aksi takdirde 0 döndürür.

Fty2 türü bağımsız değişken türleri T1, T2, ..., TN için çağrılabilir ve sırasıyla INVOKE(fn, t1, t2, ..., tN) lvalues fn, t1, t2, ..., tN türü Fty2, T1, T2, ..., TNiçin iyi biçimlendirilmişse ve değilse voidRet dönüş türü Ret olarak Retdönüştürülebilir.

Örnek

// std__functional__function_target.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>

int neg(int val)
    {
    return (-val);
    }

int main()
    {
    typedef int (*Myfun)(int);
    std::function<int (int)> fn0(neg);
    std::cout << std::boolalpha << "empty == " << !fn0 << std::endl;
    std::cout << "no target == " << (fn0.target<Myfun>() == 0) << std::endl;

    Myfun *fptr = fn0.target<Myfun>();
    std::cout << "val == " << (*fptr)(3) << std::endl;

    std::function<int (int)> fn1;
    std::cout << std::boolalpha << "empty == " << !fn1 << std::endl;
    std::cout << "no target == " << (fn1.target<Myfun>() == 0) << std::endl;

    return (0);
    }
empty == false
no target == false
val == -3
empty == true
no target == true

target_type

Çağrılabilen nesnedeki tür bilgilerini alır.

const std::type_info& target_type() const;

Açıklamalar

Üye işlevi boşsa *this döndürürtypeid(void), aksi takdirde döndürürtypeid(T); burada T hedef nesnenin türüdür.

Örnek

// std__functional__function_target_type.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>

int neg(int val)
    {
    return (-val);
    }

int main()
    {
    std::function<int (int)> fn0(neg);
    std::cout << std::boolalpha << "empty == " << !fn0 << std::endl;
    std::cout << "type == " << fn0.target_type().name() << std::endl;

    std::function<int (int)> fn1;
    std::cout << std::boolalpha << "empty == " << !fn1 << std::endl;
    std::cout << "type == " << fn1.target_type().name() << std::endl;

    return (0);
    }
empty == false
type == int (__cdecl*)(int)
empty == true
type == void