std::function::target(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 T is not Callable
    • Correct behavior: behavior is defined (always returns nullptr)

🌈 See also 🌈

  • target_type β€” obtains the typeid of 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