deductiongu...td::function(3)

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

πŸ’– NAME πŸ’–

deductionguidesforstd::function β€” deductionguidesforstd::function. Yep, the page says it twice, and honestly? Iconic.

This is the deductiongu...td::function(3) page from the C++ Standard Libary (spelled exactly like that in the header, no cap), and it's all about the deduction guides for std::function. 🫢

✨ Synopsis ✨

Defined in header <functional>
template< class R, class... ArgTypes >                     (1) (since C++17)
function( R(*)(ArgTypes...) ) -> function<R(ArgTypes...)>;
template< class F >                                        (2) (since C++17)
function( F ) -> function</*see below*/>;
template< class F >                                        (3) (since C++23)
function( F ) -> function</*see below*/>;
template< class F >                                        (4) (since C++23)
function( F ) -> function</*see below*/>;
  1. Guide (1) β€” this deduction guide is provided for std::function to allow deduction from functions. Slay.
  2. Guide (2) β€” this overload participates in overload resolution only if &F::operator() is well-formed when treated as an unevaluated operand and decltype(&F::operator()) is of the form R(G::*)(A...) (optionally cv-qualified, optionally noexcept, optionally lvalue reference qualified). The deduced type is std::function<R(A...)>. πŸ’…
  3. Guide (3) β€” this overload participates in overload resolution only if &F::operator() is well-formed when treated as an unevaluated operand and F::operator() is an explicit object parameter function whose type is of form R(G, A...) or R(G, A...) noexcept. The deduced type is std::function<R(A...)>. ✨
  4. Guide (4) β€” this overload participates in overload resolution only if &F::operator() is well-formed when treated as an unevaluated operand and F::operator() is a static member function whose type is of form R(A...) or R(A...) noexcept. The deduced type is std::function<R(A...)>. πŸ¦‹

🧠 Notes 🧠

No cap, these deduction guides do not allow deduction from a function with ellipsis parameter, and the ... in the types is always treated as a pack expansion. πŸ’€

The type deduced by these deduction guides may change in a later standard revision (in particular, this might happen if noexcept support is added to std::function in a later standard). So like, future-you has been warned. πŸ”₯

πŸŽ€ Example πŸŽ€

// Run this code

#include <functional>
int func(double) { return 0; }
int main() {
  std::function f{func}; // guide #1 deduces function<int(double)>
  int i = 5;
  std::function g = [&](double) { return i; }; // guide #2 deduces function<int(double)>
}

🩹 Defect reports 🩹

The following behavior-changing defect reports were applied retroactively to previously published C++ standards. Time-travel bug fixes, we love to see it. 🧚

  • DR LWG 3238 β€” applied to C++17.
    • Behavior as published: behavior of (2) was unclear when F::operator() is &&-qualified.
    • Correct behavior: clarified to be excluded from overload resolution.

Okay bestie, that's the page. Source/version/date from the footer: http://cppreference.com, 2024.06.10, and deductiongu...td::function(3). Go deduce something iconic πŸ’…βœ¨

πŸ“¦ where this page comes from