std::copyable_function(3)

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

πŸ’– NAME πŸ’–

std::copyable_function - std::copyable_function

okay so bestie, this is the std::copyable_function(3) page from the C++ Standard Libary β€” yes, that’s how the manual spells it, and we’re keeping the lore πŸ’….


✨ Synopsis ✨

Defined in header <functional>
template< class... >                                   (1) (since C++26)
class copyable_function; // not defined
template< class R, class... Args >

class copyable_function<R(Args...)>;
template< class R, class... Args >
class copyable_function<R(Args...) noexcept>;
template< class R, class... Args >
class copyable_function<R(Args...) &>;
template< class R, class... Args >
class copyable_function<R(Args...) & noexcept>;
template< class R, class... Args >
class copyable_function<R(Args...) &&>;
template< class R, class... Args >
class copyable_function<R(Args...) && noexcept>;       (2) (since C++26)
template< class R, class... Args >
class copyable_function<R(Args...) const>;
template< class R, class... Args >
class copyable_function<R(Args...) const noexcept>;
template< class R, class... Args >
class copyable_function<R(Args...) const &>;
template< class R, class... Args >
class copyable_function<R(Args...) const & noexcept>;
template< class R, class... Args >
class copyable_function<R(Args...) const &&>;
template< class R, class... Args >

class copyable_function<R(Args...) const && noexcept>;

okay so what even is it? The class template std::copyable_function is a general-purpose polymorphic function wrapper β€” and no cap, it’s giving main character energy for callables. std::copyable_function objects can store and invoke any CopyConstructible Callable target β€” functions, lambda expressions, bind expressions, or other function objects, as well as pointers to member functions and pointers to member objects. 🫢

The stored callable object is called the target of std::copyable_function. If a std::copyable_function contains no target, it is called empty. And this part is dramatic, bestie: unlike std::function, invoking an empty std::copyable_function results in undefined behavior. 😭 That is a β€œwe don’t do that here” situation, fr fr.

std::copyable_functions supports every possible combination of cv-qualifiers, ref-qualifiers, and noexcept-specifiers not including volatile provided in its template parameter. These qualifiers and specifier (if any) are added to its operator(). ✨

std::copyable_function satisfies the requirements of CopyConstructible and CopyAssignable. πŸ’–


πŸŽ€ Member types πŸŽ€

  • result_type β€” R

πŸ’… Member functions πŸ’…

  • constructor (C++26) β€” constructs a new std::copyable_function object (public member function)
  • destructor (C++26) β€” destroys a std::copyable_function object (public member function)
  • operator= (C++26) β€” replaces or destroys the target (public member function)
  • swap (C++26) β€” swaps the targets of two std::copyable_function objects (public member function)
  • operator bool (C++26) β€” checks if the std::copyable_function has a target (public member function)
  • operator() (C++26) β€” invokes the target (public member function)

🫢 Non-member functions 🫢

  • swap(std::copyable_function) (C++26) β€” overloads the std::swap algorithm (function)
  • operator== (C++26) β€” compares a std::copyable_function with nullptr (function)

πŸ“ Notes πŸ“

Implementations may store a callable object of small size within the std::copyable_function object. Such small object optimization is effectively required for function pointers and std::reference_wrapper specializations, and can only be applied to types T for which std::is_nothrow_move_constructible_v<T> is true. 🧚

Feature-test macro           Value    Std      Feature
__cpp_lib_copyable_function  202306L  (C++26)  std::copyable_function

🌸 Example 🌸

This section is incomplete Reason: example

the incomplete iconique energy is real πŸ™ˆ


πŸ‘€ 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)

🏷️ Category 🏷️

  • Todo with reason

okay that’s the whole page, bestie πŸ’– signed off with love from http://cppreference.com, dated 2024.06.10 β€” mwah πŸ«ΆπŸ’‹

πŸ“¦ where this page comes from