Namespaces
Variants

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

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* )
template < class T >
T * target ( ) noexcept ;
(1) (C++11 이후)
template < class T >
const T * target ( ) const noexcept ;
(2) (C++11 이후)

저장된 호출 가능 함수 대상에 대한 포인터를 반환합니다.

목차

매개변수

(없음)

반환값

저장된 함수에 대한 포인터를 반환합니다. target_type ( ) == typeid ( T ) 조건이 참일 경우, 그렇지 않으면 널 포인터를 반환합니다.

예제

#include <functional>
#include <iostream>
int f(int, int) { return 1; }
int g(int, int) { return 2; }
void test(std::function<int(int, int)> const& arg)
{
    std::cout << "test function: ";
    if (arg.target<std::plus<int>>())
        std::cout << "it is plus\n";
    if (arg.target<std::minus<int>>())
        std::cout << "it is minus\n";
    int (*const* ptr)(int, int) = arg.target<int(*)(int, int)>();
    if (ptr && *ptr == f)
        std::cout << "it is the function f\n";
    if (ptr && *ptr == g)
        std::cout << "it is the function g\n";
}
int main()
{
    test(std::function<int(int, int)>(std::plus<int>()));
    test(std::function<int(int, int)>(std::minus<int>()));
    test(std::function<int(int, int)>(f));
    test(std::function<int(int, int)>(g));
}

출력:

test function: it is plus
test function: it is minus
test function: it is the function f
test function: it is the function g

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 2591 C++11 T Callable 이 아닌 경우 동작이 정의되지 않음 동작이 정의됨 (항상 nullptr 을 반환)

참고 항목

저장된 대상의 typeid 를 획득함
(public member function)