deduction guides for std::function
From cppreference.com
< cpp | utility | functional | function
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Relational operators (deprecated in C++20)
Integer comparison functions
Swap and type operations
Common vocabulary types
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
Function objects
Old binders and adaptors
(C++11)
(C++23)
(C++26)
(C++26)
(C++11)
(C++11)
(C++20)(C++23)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++20)(C++20)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++17)
(C++17)
(C++17)
(C++17)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(until C++20*)
(until C++20*)
(until C++17*)(until C++17*)
(until C++17*)(until C++17*)
(until C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(until C++20*)
(until C++20*)
std::function
(until C++17)
(until C++20)
(until C++17)
Deduction guides(C++17)
Defined in header
<functional>
template< class R, class... ArgTypes >
function( R(*)(ArgTypes...) ) -> function<R(ArgTypes...)>;
(1)
(since C++17)
function( R(*)(ArgTypes...) ) -> function<R(ArgTypes...)>;
template< class F >
function( F ) -> function</*see below*/>;
(2)
(since C++17)
function( F ) -> function</*see below*/>;
template< class F >
function( F ) -> function</*see below*/>;
(3)
(since C++23)
function( F ) -> function</*see below*/>;
template< class F >
function( F ) -> function</*see below*/>;
(4)
(since C++23)
function( F ) -> function</*see below*/>;
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) 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) 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...)>.
[edit] Notes
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).
[edit] 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)> }
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 3238 | C++17 | behavior of (2) was unclear when F::operator() is &&-qualified |
clarified to be excluded from overload resolution |