Spec-Zone.ru › C++

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

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)

См. также

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

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/utility/functional/function/target

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API