Namespaces
Variants

std::move_only_function:: 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 ) /*cv*/ /*ref*/ noexcept ( /*noex*/ ) ;
(C++23 이후)

저장된 호출 가능 대상(callable target)을 매개변수 args 로 호출합니다. /*cv*/ , /*ref*/ , 그리고 /*noex*/ 부분들은 operator ( ) 의 해당 부분들이 std::move_only_function 의 템플릿 매개변수와 동일합니다.

다음 코드와 동등합니다: return std:: invoke_r < R > ( /*cv-ref-cast*/ ( f ) , std:: forward < Args > ( args ) ... ) ; , 여기서 f * this 의 대상 객체를 나타내는 cv-unqualified 좌측값이며, /*cv-ref-cast*/ ( f ) 는 다음과 동등합니다:

  • f 만약 cv ref 가 비어 있거나 & 인 경우, 또는
  • std:: as_const ( f ) 만약 cv ref const 또는 const & 인 경우, 또는
  • std :: move ( f ) 만약 cv ref && 인 경우, 또는
  • std :: move ( std:: as_const ( f ) ) 만약 cv ref const && 인 경우.

* this 가 비어 있을 경우 동작은 정의되지 않습니다.

목차

매개변수

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

반환값

std:: invoke_r < R > ( /*cv-ref-cast*/ ( f ) , std:: forward < Args > ( args ) ... ) .

예외

기본 함수 호출에서 발생한 예외를 전파합니다.

예제

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

#include <iostream>
#include <functional>
void call(std::move_only_function<int() const> f)  // can be passed by value
{ 
    std::cout << f() << '\n';
}
int normal_function() 
{
    return 42;
}
int main()
{
    int n = 1;
    auto lambda = [&n](){ return n; };
    std::move_only_function<int() const> f = lambda;
    call(std::move(f));
    n = 2;
    call(lambda); 
    f = normal_function; 
    call(std::move(f));
}

출력:

1
2
42

참고 항목

대상 호출
( std::function<R(Args...)> 의 public 멤버 함수)
저장된 함수 호출
( std::reference_wrapper<T> 의 public 멤버 함수)
(C++17) (C++23)
주어진 인수로 모든 Callable 객체 호출 및 반환 타입 지정 가능 (C++23부터)
(함수 템플릿)