Namespaces
Variants

std::function<R(Args...)>:: operator()

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
R operator ( ) ( Args... args ) const ;
(C++11부터)

저장된 호출 가능 함수 대상을 args 매개변수로 호출합니다.

효과적으로 INVOKE<R> ( f, std:: forward < Args > ( args ) ... ) 를 수행합니다. 여기서 f 대상 객체 입니다. * this

목차

매개변수

args - 저장된 호출 가능 함수 대상에 전달할 매개변수

반환값

R void 인 경우 None. 그렇지 않으면 저장된 호출 가능 객체의 호출 반환값.

예외

호출 가능한 함수 대상을 저장하지 않는 경우, 즉 ! * this == true 인 경우 std::bad_function_call 을 발생시킵니다.

예제

다음 예제는 std::function 이 값으로 다른 함수에 전달될 수 있는 방법을 보여줍니다. 또한 std::function 이 람다를 저장할 수 있는 방법도 보여줍니다.

#include <functional>
#include <iostream>
void call(std::function<int()> f) // can be passed by value
{ 
    std::cout << f() << '\n';
}
int normal_function()
{
    return 42;
}
int main()
{
    int n = 1;
    std::function<int()> f;
    try
    {
        call(f);
    }
    catch (const std::bad_function_call& ex)
    {
        std::cout << ex.what() << '\n';
    }
    f = [&n](){ return n; };
    call(f);
    n = 2;
    call(f);
    f = normal_function;
    call(f);
    std::function<void(std::string, int)> g;
    g = [](std::string str, int i) { std::cout << str << ' ' << i << '\n'; };
    g("Hi", 052);
}

가능한 출력:

bad_function_call
1
2
42
Hi 42

참고 항목

대상을 호출함
( std::move_only_function 의 public member function)
저장된 함수를 호출함
( std::reference_wrapper<T> 의 public member function)
std::function 을 호출할 때 발생하는 예외
(class)
(C++17) (C++23)
주어진 인수로 모든 Callable 객체를 호출함 (반환 타입 지정 가능) (C++23부터)
(function template)