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 Applied to Behavior as published Correct behavior
LWG 2591 C++11 the behavior is undefined if T is not Callable behavior is defined (always returns nullptr )

Смотрите также

получает typeid сохранённой цели
(публичная функция-член)