Difference between revisions of "cpp/named req/Callable"
From cppreference.com
(explicitly mention smart pointers) |
m (added links to std::function_ref and std::copyable_function) |
||
(34 intermediate revisions by 11 users not shown) | |||
Line 1: | Line 1: | ||
− | {{cpp/ | + | {{cpp/named req/title|Callable}} |
− | {{cpp/ | + | {{cpp/named req/navbar}} |
− | A {{ | + | A {{named req|Callable}} type is a type for which the {{lti|cpp/utility/functional|INVOKE}} and {{lti|cpp/utility/functional|INVOKE<R>}} operations (used by, e.g., {{lc|std::function}}, {{lc|std::bind}}, and {{lc|std::thread::thread}}) are applicable. |
− | == | + | {{rev begin}} |
+ | {{rev|since=c++17| | ||
+ | {{tti|INVOKE}} can be performed explicitly using the library function {{lc|std::invoke}}. | ||
+ | }} | ||
+ | {{rev|since=c++23| | ||
+ | {{tti|INVOKE<R>}} can be performed explicitly using the library function {{ltt|cpp/utility/functional/invoke|std::invoke_r}}. | ||
+ | }} | ||
+ | {{rev end}} | ||
− | The type {{tt|T}} satisfies {{ | + | ===Requirements=== |
+ | The type {{tt|T}} satisfies {{named req/core|Callable}} if | ||
Given | Given | ||
− | * {{tt|f}}, an object of type {{tt|T}} | + | * {{tt|f}}, an object of type {{tt|T}}, |
− | * {{tt|ArgTypes}}, suitable argument | + | * {{tt|ArgTypes}}, suitable list of argument types, |
− | * {{tt|R}}, suitable return type | + | * {{tt|R}}, suitable return type. |
The following expressions must be valid: | The following expressions must be valid: | ||
Line 19: | Line 27: | ||
!Expression||Requirements | !Expression||Requirements | ||
|- | |- | ||
− | | {{ | + | |{{box|{{lti|cpp/utility/functional|INVOKE<R>}}{{c/core|(f, std::declval<ArgTypes>()...)}}}} |
− | | | + | |The expression is well-formed in unevaluated context. |
|} | |} | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
===Notes=== | ===Notes=== | ||
− | + | [[cpp/language/pointer#Pointers to data members|Pointers to data members]] are {{named req/core|Callable}}, even though no function calls take place. | |
− | + | ||
− | + | ||
− | [[cpp/language/pointer# | + | |
===Standard library=== | ===Standard library=== | ||
− | In addition, the following standard library facilities accept any {{ | + | In addition, the following standard library facilities accept any {{named req/core|Callable}} type (not just {{named req|FunctionObject}}): |
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc | {{ | + | {{dsc inc|cpp/utility/functional/dsc function}} |
− | {{dsc | {{ | + | {{dsc inc|cpp/utility/functional/dsc move_only_function}} |
− | {{dsc | {{ | + | {{dsc inc|cpp/utility/functional/dsc copyable_function}} |
− | {{dsc | | + | {{dsc inc|cpp/utility/functional/dsc function_ref}} |
− | {{dsc | | + | {{dsc inc|cpp/utility/functional/dsc bind}} |
− | {{dsc | | + | {{dsc inc|cpp/utility/functional/dsc bind_front}} |
− | {{dsc | | + | {{dsc inc|cpp/utility/functional/dsc reference_wrapper}} |
− | {{dsc | | + | {{dsc inc|cpp/types/dsc result_of}} |
+ | {{dsc inc|cpp/thread/dsc thread}} | ||
+ | {{dsc inc|cpp/thread/dsc jthread}} | ||
+ | {{dsc inc|cpp/thread/dsc call_once}} | ||
+ | {{dsc inc|cpp/thread/dsc async}} | ||
+ | {{dsc inc|cpp/thread/dsc packaged_task}} | ||
{{dsc end}} | {{dsc end}} | ||
+ | |||
+ | ===See also=== | ||
+ | {{dsc begin}} | ||
+ | {{dsc inc|cpp/types/dsc is_invocable}} | ||
+ | {{dsc inc|cpp/concepts/dsc invocable}} | ||
+ | {{dsc inc|cpp/utility/functional/dsc invoke}} | ||
+ | {{dsc end}} | ||
+ | |||
+ | {{langlinks|de|en|es|ja|zh}} |
Latest revision as of 23:56, 29 July 2024
A Callable type is a type for which the INVOKE and INVOKE<R> operations (used by, e.g., std::function, std::bind, and std::thread::thread) are applicable.
|
(since C++17) |
|
(since C++23) |
Contents |
[edit] Requirements
The type T
satisfies Callable if
Given
-
f
, an object of typeT
, -
ArgTypes
, suitable list of argument types, -
R
, suitable return type.
The following expressions must be valid:
Expression | Requirements |
---|---|
INVOKE<R>(f, std::declval<ArgTypes>()...) | The expression is well-formed in unevaluated context. |
[edit] Notes
Pointers to data members are Callable, even though no function calls take place.
[edit] Standard library
In addition, the following standard library facilities accept any Callable type (not just FunctionObject):
(C++11) |
copyable wrapper of any copy constructible callable object (class template) |
(C++23) |
move-only wrapper of any callable object that supports qualifiers in a given call signature (class template) |
(C++26) |
copyable wrapper of any copy constructible callable object that supports qualifiers in a given call signature (class template) |
(C++26) |
non-owning wrapper of any callable object (class template) |
(C++11) |
binds one or more arguments to a function object (function template) |
(C++20)(C++23) |
bind a variable number of arguments, in order, to a function object (function template) |
(C++11) |
CopyConstructible and CopyAssignable reference wrapper (class template) |
(C++11)(removed in C++20)(C++17) |
deduces the result type of invoking a callable object with a set of arguments (class template) |
(C++11) |
manages a separate thread (class) |
(C++20) |
std::thread with support for auto-joining and cancellation (class) |
(C++11) |
invokes a function only once even if called from multiple threads (function template) |
(C++11) |
runs a function asynchronously (potentially in a new thread) and returns a std::future that will hold the result (function template) |
(C++11) |
packages a function to store its return value for asynchronous retrieval (class template) |
[edit] See also
checks if a type can be invoked (as if by std::invoke) with the given argument types (class template) | |
(C++20) |
specifies that a callable type can be invoked with a given set of argument types (concept) |
(C++17)(C++23) |
invokes any Callable object with given arguments and possibility to specify return type(since C++23) (function template) |