std::ptr_fun(3)

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

πŸŽ€ std::ptr_fun(3) β€” C++ Standard Libary edition πŸŽ€

🌸 NAME 🌸

std::ptr_fun β€” std::ptr_fun. That's the name, bestie, and honestly? Iconic minimalism. πŸ’…


✨ Synopsis ✨

Defined in header <functional> β€” yes, the <functional> girlies. πŸŽ€

template< class Arg, class Result >
                                                                (deprecated in C++11)
          std::pointer_to_unary_function<Arg,Result>        (1) (removed in C++17)

              ptr_fun( Result (*f)(Arg) );
template< class Arg1, class Arg2, class Result >
                                                                (deprecated in C++11)
          std::pointer_to_binary_function<Arg1,Arg2,Result> (2) (removed in C++17)

              ptr_fun( Result (*f)(Arg1, Arg2) );

Okay so what does it actually do? It creates a function wrapper object (either std::pointer_to_unary_function or std::pointer_to_binary_function), deducing the target type from the template arguments. Slay? Kinda. ✨

  1. Effectively calls std::pointer_to_unary_function<Arg,Result>(f).
  2. Effectively calls std::pointer_to_binary_function<Arg1,Arg2,Result>(f).
🚨 Warning bestie: this function and the related types are deprecated as of C++11 in favor of the more general std::function and std::ref, both of which create callable adaptor-compatible function objects from plain functions. So it's giving legacy energy, not new-code energy. πŸ’€

πŸ“ Parameters πŸ“

  • f β€” pointer to a function to create a wrapper for. That's it, that's the param. 🫢

🌈 Return value 🌈

A function object wrapping f. Cute, right? πŸ’–


😭 Exceptions 😭

May throw implementation-defined exceptions. No cap, that's what the manual says, so guard your vibes accordingly. πŸ™ˆ


πŸ’… Example πŸ’…

// Run this code

        #include <algorithm>
        #include <functional>
        #include <iostream>
        #include <string_view>

        constexpr bool is_vowel(char c)
        {
            return std::string_view{"aeoiuAEIOU"}.find(c) !=
       std::string_view::npos;
        }

        int main()
        {
            std::string_view s = "Hello, world!";
            std::ranges::copy_if(s, std::ostreambuf_iterator<char>(std::cout),
                std::not1(std::ptr_fun(is_vowel)));
        #if 0
        // C++11 alternatives:
                std::not1(std::cref(is_vowel)));
                std::not1(std::function<bool(char)>(is_vowel)));
                [](char c) { return !is_vowel(c); });
        // C++17 alternatives:
                std::not_fn(is_vowel));
        #endif
        }

🌟 Output 🌟

Hll, wrld!

Not the vowel erasure we asked for, but the vowel erasure we deserve. πŸ˜­πŸ’–


πŸ”₯ See also πŸ”₯

  • function (C++11) β€” wraps callable object of any copy constructible type with specified function call signature (class template).
  • move_only_function (C++23) β€” wraps callable object of any type with specified function call signature (class template).
  • invoke (C++17), invoke_r (C++23) β€” invokes any Callable object with given arguments and possibility to specify return type (since C++23) (function template).
  • not_fn (C++17) β€” creates a function object that returns the complement of the result of the function object it holds (function template).

πŸ’Œ from http://cppreference.com, dated 2024.06.10 β€” std::ptr_fun(3) signing off, periodt. 🫢

πŸ“¦ where this page comes from