function 클래스

호출 가능 개체용 래퍼입니다.

구문

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

매개 변수

Fty
래핑할 함수 형식입니다.

도끼
할당자 함수입니다.

설명

클래스 템플릿은 호출 서명이 있는 호출 래퍼입니다 Ret(T1, T2, ..., TN). 이를 사용하여 다양한 호출 가능한 개체를 균일한 래퍼에 묶습니다.

일부 멤버 함수는 원하는 대상 개체를 명명하는 피연산자를 사용합니다. 그러한 피연산자를 여러 방법으로 지정할 수 있습니다.

fn: 호출 가능한 개체 fn입니다. 호출 후 개체는 function 다음 복사본을 보유합니다. fn

fnref: 호출 후 개체가 에 대한 참조를 보유하는 function ;에 의해 fnref.get()명명된 호출 가능한 개체입니다.fnref.get()

right: 개체가 보유한 function 호출 가능한 개체(있는 경우) right

npc: null 포인터; 호출 후 개체가 function 비어 있음

모든 경우INVOKE(f, t1, t2, ..., tN)에서 호출 가능한 개체이고 t1, t2, ..., tN 형식의 lvalues인 경우 f 형식은 T1, T2, ..., TN 올바른 형식이어야 하며, void가 아닌 경우 Ret 으로 변환할 Ret수 있어야 합니다.

function 개체에는 호출 가능한 개체 또는 호출 가능한 개체에 대한 참조가 없습니다.

멤버

생성자

이름 설명
function 비어 있거나 고정된 시그니처가 포함된 임의 형식의 호출 가능 개체를 저장하는 래퍼를 생성합니다.

Typedef

이름 설명
result_type 호출 가능 개체의 반환 형식입니다.

함수

속성 설명
assign 이 함수 개체에 호출 가능 개체를 할당합니다.
swap 두 개의 호출 가능 개체를 바꿉니다.
target 저장된 호출 가능 개체가 지정된 대로 호출 가능한지 테스트합니다.
target_type 호출 가능 개체에 대한 형식 정보를 가져옵니다.

연산자

이름 설명
지정되지 않은 연산자 저장된 호출 가능 개체가 있는지 테스트합니다.
operator() 호출 가능 개체를 호출합니다.
operator= 저장된 호출 가능 개체를 바꿉니다.

할당

이 함수 개체에 호출 가능 개체를 할당합니다.

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

매개 변수

_Func
호출 가능 개체입니다.

_Fnref
호출 가능 개체가 포함된 참조 래퍼입니다.

도끼
할당자 개체입니다.

설명

멤버 함수는 각각 *this에 의해 포함된 callable objectoperand로 전달된 호출 가능 개체로 바꿉니다. 둘 다 할당자 개체 Ax를 사용하여 스토리지를 할당합니다.

function

비어 있거나 고정된 시그니처가 포함된 임의 형식의 호출 가능 개체를 저장하는 래퍼를 생성합니다.

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

매개 변수

right
복사할 함수 개체입니다.

Fx
호출 가능 개체의 형식입니다.

_Func
래핑할 호출 가능 개체입니다.

Alloc
할당자 형식입니다.

도끼
할당자입니다.

_Fnref
래핑할 호출 가능 개체 참조입니다.

설명

처음 두 개의 생성자는 빈 function 개체를 생성합니다. 다음 세 개의 생성자는 피연산자로 전달된 호출 가능 개체를 포함하는 function 개체를 생성합니다. 마지막 두 개의 생성자는 할당자 개체 Ax를 사용하여 스토리지를 할당합니다.

예시

// 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).

지정되지 않은 연산자

저장된 호출 가능 개체가 있는지 테스트합니다.

operator unspecified();

설명

연산자는 개체가 비어 있지 않은 경우에만 true 값으로 변환할 수 bool 있는 값을 반환합니다. 이를 사용하여 개체가 비어 있는지 테스트합니다.

예시

// 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()

호출 가능 개체를 호출합니다.

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

매개 변수

테네시
N번째 인수의 형식입니다.

테네시
N번째 호출 인수입니다.

설명

멤버 함수는 INVOKE(fn, t1, t2, ..., tN, Ret)를 반환합니다. 여기서 fn*this에 저장된 대상 개체입니다. 이를 사용하여 래핑된 호출 가능 개체를 호출합니다.

예시

// 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=

저장된 호출 가능 개체를 바꿉니다.

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

매개 변수

Npc
null 포인터 상수입니다.

right
복사할 함수 개체입니다.

Fn
래핑할 호출 가능 개체입니다.

fnref
래핑할 호출 가능 개체 참조입니다.

설명

멤버 함수는 각각 *this에 의해 포함된 호출 가능 개체를 피연산자로 전달된 호출 가능 개체로 바꿉니다.

예시

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

호출 가능 개체의 반환 형식입니다.

typedef Ret result_type;

설명

형식 정의는 템플릿의 호출 시그니처에서 Ret 형식의 동의어입니다. 이를 사용하여 래핑된 호출 가능 개체의 반환 형식을 결정합니다.

예시

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

두 개의 호출 가능 개체를 바꿉니다.

void swap(function& right);

매개 변수

right
바꿀 함수 개체입니다.

설명

멤버 함수는 대상 개체를 오른쪽 간에 *this교환합니다. 일정한 시간에 이 작업을 수행하고 예외를 throw하지 않습니다.

예시

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

target

저장된 호출 가능 개체가 지정된 대로 호출 가능한지 테스트합니다.

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

매개 변수

Fty2
테스트할 대상 호출 가능 개체 형식입니다.

설명

인수 형식 및 반환 형식 에 대해 Fty2 형식 T1, T2, ..., TN 을 호출할 수 있어야 Ret합니다. target_type() == typeid(Fty2)인 경우 멤버 템플릿 함수는 대상 개체의 주소를 반환하고, 그렇지 않으면 0을 반환합니다.

형식 Fty2는 인수 형식 T1, T2, ..., TN 에 대해 호출할 수 있으며, 형식의 lvalues fn, t1, t2, ..., tN 에 대해 각각 INVOKE(fn, t1, t2, ..., tN) 형식이 올바른 형식이고 그렇지 않은 void경우 변환할 수 있는 경우 Ret 반환 형식 Fty2, T1, T2, ..., TNRet 을 호출할 Ret수 있습니다.

예시

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

호출 가능 개체에 대한 형식 정보를 가져옵니다.

const std::type_info& target_type() const;

설명

멤버 함수는 *this가 비어 있으면 typeid(void)를 반환하고, 그렇지 않으면 typeid(T)를 반환합니다. 여기서 T는 대상 개체의 형식입니다.

예제

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