function::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
    줄 바꿈 호출할 개체입니다.

  • 할당
    할당자 형식입니다.

  • _Ax
    할당자입니다.

  • _Fnref
    줄 바꿈 호출 가능 개체 참조입니다.

설명

처음 두 명의 생성자 빈 생성 function 개체입니다.다음 세 명의 생성자를 생성 한 function 호출 가능 개체를 포함 하는 개체는 피연산자로 전달 합니다.마지막 두 생성자 저장소 할당자와 _Ax 개체를 할당합니다.

예제

 

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

요구 사항

헤더: <functional>

네임 스페이스: std

참고 항목

참조

function Class

function::operator=

Lvalue가 고 Rvalue