Namespaces
Variants

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

De es.cppreference.net
 
 
Biblioteca de utilidades
Soporte del lenguaje
Soporte de tipos (tipos básicos, RTTI)
Macros de prueba de características de la biblioteca (C++20)
Utilidades de programa
Funciones variádicas
Soporte de corrutinas (C++20)
Soporte de contratos (C++26)
Comparación de tres vías
(C++20)
(C++20)(C++20)(C++20)    
(C++20)(C++20)(C++20)

 
Objetos función
Invocación de funciones
(C++17)(C++23)
Objeto función identidad
(C++20)
Envoltorios de operadores transparentes
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

Antiguos enlazadores y adaptadores
(hasta C++17*)
(hasta C++17*)
(hasta C++17*)
(hasta C++17*)  
(hasta C++17*)
(hasta C++17*)(hasta C++17*)(hasta C++17*)(hasta C++17*)
(hasta C++20*)
(hasta C++20*)
(hasta C++17*)(hasta C++17*)
(hasta C++17*)(hasta C++17*)

(hasta C++17*)
(hasta C++17*)(hasta C++17*)(hasta C++17*)(hasta C++17*)
(hasta C++20*)
(hasta C++20*)
 
 
template< class T > 
T* target() noexcept;
(1) (desde C++11)
template< class T > 
const T* target() const noexcept;
(2) (desde C++11)

Devuelve un puntero al destino de la función invocable almacenada.

Parámetros

(ninguno)

Valor de retorno

Un puntero a la función almacenada si target_type ( ) == typeid ( T ) , de lo contrario un puntero nulo.

Ejemplo

#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));
}

Salida:

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

Informes de defectos

Los siguientes informes de defectos que modifican el comportamiento se aplicaron retroactivamente a los estándares publicados anteriormente de C++.

DR Aplicado a Comportamiento publicado Comportamiento correcto
LWG 2591 C++11 el comportamiento es indefinido si T no es Callable el comportamiento está definido (siempre retorna nullptr )

Véase también

obtiene el typeid del objetivo almacenado
(función miembro pública)