std::function<R(Args...)>::target
ko.cppreference.net 참조
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) |