Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/named req/Callable"

From cppreference.com
< cpp‎ | named req
m (added links to std::function_ref and std::copyable_function)
 
(19 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{cpp/concept/title|Callable}}
+
{{cpp/named req/title|Callable}}
{{cpp/concept/navbar}}
+
{{cpp/named req/navbar}}
  
A {{concept|Callable}} type is a type for which the INVOKE operation (used by, e.g.,   {{lc|std::function}}, {{lc|std::bind}}, and {{lc|std::thread::thread}}) is applicable. {{rev inl|since=c++17|This operation may be performed explicitly using the library function {{lc|std::invoke}}.}}
+
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}}
  
 
===Requirements===
 
===Requirements===
 
+
The type {{tt|T}} satisfies {{named req/core|Callable}} if
The type {{tt|T}} satisfies {{tt|Callable}} if
+
  
 
Given
 
Given
* {{tt|f}}, an object of type {{tt|T}}
+
* {{tt|f}}, an object of type {{tt|T}},
* {{tt|ArgTypes}}, suitable list of argument types
+
* {{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
 
|-
 
|-
| {{c|1=INVOKE<R>(f, std::declval<ArgTypes>()...)}}
+
|{{box|{{lti|cpp/utility/functional|INVOKE<R>}}{{c/core|(f, std::declval<ArgTypes>()...)}}}}
| the expression is well-formed in unevaluated context
+
|The expression is well-formed in unevaluated context.
 
|}
 
|}
 
where ''INVOKE<R>(f, t1, t2, ..., tN)'' is defined as {{c|static_cast<void>(INVOKE(f, t1, t2, ..., tN))}} if {{tt|R}} is possibly cv-qualified {{c|void}}, otherwise ''INVOKE(f, t1, t2, ..., tN)'', [[cpp/language/implicit_cast|implicitly converted]] to {{tt|R}}
 
 
where ''INVOKE(f, t1, t2, ..., tN)'' is defined as follows:
 
* if {{tt|f}} is a [[cpp/language/pointer#Pointers_to_member_functions|pointer to member function]] of class {{tt|T}}:
 
:* If {{c|1=std::is_base_of<T, std::decay_t<decltype(t1)>>::value}} is {{c|true}}, then {{c|INVOKE(f, t1, t2, ..., tN)}} is equivalent to {{c|(t1.*f)(t2, ..., tN)}}
 
:* {{rev inl|since=c++17|otherwise, if {{c|std::decay_t<decltype(t1)>}} is a specialization of {{lc|std::reference_wrapper}}, then {{c|INVOKE(f, t1, t2, ..., tN)}} is equivalent to {{c|(t1.get().*f)(t2, ..., tN)}}}}
 
:* otherwise, if {{tt|t1}} does not satisfy the previous items, then {{c|INVOKE(f, t1, t2, ..., tN)}} is equivalent to {{c|((*t1).*f)(t2, ..., tN)}}.
 
* otherwise, if N == 1 and {{tt|f}} is a [[cpp/language/pointer#Pointers_to_data_members|pointer to data member]] of class {{tt|T}}:
 
:* If {{c|1=std::is_base_of<T, std::decay_t<decltype(t1)>>::value}} is {{c|true}}, then {{c|INVOKE(f, t1)}} is equivalent to {{c|t1.*f}}
 
:* {{rev inl|since=c++17|1=otherwise, if {{c|std::decay_t<decltype(t1)>}} is a specialization of {{lc|std::reference_wrapper}}, then {{c|INVOKE(f, t1)}} is equivalent to {{c|t1.get().*f}}}}
 
:* otherwise, if {{tt|t1}}  does not satisfy the previous items, then {{c|INVOKE(f, t1)}} is equivalent to {{c|(*t1).*f}}
 
* otherwise, {{c|INVOKE(f, t1, t2, ..., tN)}} is equivalent to {{c|f(t1, t2, ..., tN)}} (that is, {{tt|f}} is a {{concept|FunctionObject}})
 
  
 
===Notes===
 
===Notes===
For pointers to member functions and pointers to data members, {{tt|t1}} may be a regular pointer or an object of class type that overloads {{tt|operator*}}, such as {{lc|std::unique_ptr}} or {{lc|std::shared_ptr}}.
+
[[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#Pointers_to_data_members|Pointers to data members]] are {{tt|Callable}}, even though no function calls take place.
+
  
 
===Standard library===
 
===Standard library===
In addition, the following standard library facilities accept any {{concept|Callable}} type (not just {{concept|FunctionObject}})
+
In addition, the following standard library facilities accept any {{named req/core|Callable}} type (not just {{named req|FunctionObject}}):
  
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc | {{lc|std::function}} }}
+
{{dsc inc|cpp/utility/functional/dsc function}}
{{dsc | {{lc|std::bind}} }}
+
{{dsc inc|cpp/utility/functional/dsc move_only_function}}
{{dsc | {{lc|std::result_of}} }}
+
{{dsc inc|cpp/utility/functional/dsc copyable_function}}
{{dsc | {{lc|std::thread::thread}} }}
+
{{dsc inc|cpp/utility/functional/dsc function_ref}}
{{dsc | {{lc|std::call_once}} }}
+
{{dsc inc|cpp/utility/functional/dsc bind}}
{{dsc | {{lc|std::async}} }}
+
{{dsc inc|cpp/utility/functional/dsc bind_front}}
{{dsc | {{lc|std::packaged_task}} }}
+
{{dsc inc|cpp/utility/functional/dsc reference_wrapper}}
{{dsc | {{lc|std::reference_wrapper}} }}
+
{{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}}
 
===Defect reports===
 
{{dr list begin}}
 
{{dr list item|wg=lwg|dr=2420|std=C++11|before=when R is void, the result must be implicitly convertible to void (which is impossible)|after=the result is explicitly converted to void when R is cv void}}
 
{{dr list end}}
 
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/types/dsc is_invocable}}
+
{{dsc inc|cpp/types/dsc is_invocable}}
 +
{{dsc inc|cpp/concepts/dsc invocable}}
 +
{{dsc inc|cpp/utility/functional/dsc invoke}}
 
{{dsc end}}
 
{{dsc end}}
  
[[zh:cpp/concept/Callable]]
+
{{langlinks|de|en|es|ja|zh}}

Latest revision as of 23:56, 29 July 2024

 
 
C++ named requirements
 

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.

INVOKE can be performed explicitly using the library function std::invoke.

(since C++17)

INVOKE<R> can be performed explicitly using the library function std::invoke_r.

(since C++23)

Contents

[edit] Requirements

The type T satisfies Callable if

Given

  • f, an object of type T,
  • 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) [edit]
move-only wrapper of any callable object that supports qualifiers in a given call signature
(class template) [edit]
copyable wrapper of any copy constructible callable object that supports qualifiers in a given call signature
(class template) [edit]
non-owning wrapper of any callable object
(class template) [edit]
(C++11)
binds one or more arguments to a function object
(function template) [edit]
(C++20)(C++23)
bind a variable number of arguments, in order, to a function object
(function template) [edit]
CopyConstructible and CopyAssignable reference wrapper
(class template) [edit]
(C++11)(removed in C++20)(C++17)
deduces the result type of invoking a callable object with a set of arguments
(class template) [edit]
(C++11)
manages a separate thread
(class) [edit]
(C++20)
std::thread with support for auto-joining and cancellation
(class) [edit]
(C++11)
invokes a function only once even if called from multiple threads
(function template) [edit]
(C++11)
runs a function asynchronously (potentially in a new thread) and returns a std::future that will hold the result
(function template) [edit]
packages a function to store its return value for asynchronous retrieval
(class template) [edit]

[edit] See also

checks if a type can be invoked (as if by std::invoke) with the given argument types
(class template) [edit]
specifies that a callable type can be invoked with a given set of argument types
(concept) [edit]
(C++17)(C++23)
invokes any Callable object with given arguments and possibility to specify return type(since C++23)
(function template) [edit]