std::function::target(3)
π
std::function::target(3) π
okay bestie, this is the page for std::function::target(3) from the C++ Standard Libary β yes, thatβs the manual name, weβre keeping it exactly as it is, no cap π
. Itβs giving βniche C++ member function but make it a vibe check.β Letβs go.
β¨ NAME β¨
std::function::target β std::function::target. literally that. iconic. periodt.
β¨ Synopsis β¨
Both overloads are since C++11, fr fr:
template< class T > (1) (since C++11)
T* target() noexcept;
template< class T > (2) (since C++11)
const T* target() const noexcept;
Returns a pointer to the stored callable function target.
πΈ Parameters πΈ
None. (none) β no parameters, bestie π«Ά.
π Return value π
A pointer to the stored function if target_type() == typeid(T), otherwise a null pointer. Thatβs the whole return vibe.
π Example π
okay so hereβs the code, copied exactly because we do not mess with code in this house π«Ά:
// Run this code
#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));
}
π Output π
test function: it is plus
test function: it is minus
test function: it is the function f
test function: it is the function g
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. ngl, retroactive lore drop.
- DR:
LWG 2591- Applied to:
C++11 - Behavior as published: the behavior is undefined if
Tis notCallable - Correct behavior: behavior is defined (always returns
nullptr)
- Applied to:
π See also π
target_typeβ obtains thetypeidof the stored target (public member function).
okay thatβs the whole page, bestie π source/version: http://cppreference.com Β· date: 2024.06.10 Β· periodt. π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms