A Callable type is a type for which the INVOKE operation (used by, e.g., std::function
, std::bind
, and std::thread::thread
) is applicable. This operation may be performed explicitly using the library function std::invoke
. (since C++17).
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 |
where INVOKE<R>(f, t1, t2, ..., tN) is defined as static_cast<void>(INVOKE(f, t1, t2, ..., tN))
if R
is possibly cv-qualified void
, otherwise INVOKE(f, t1, t2, ..., tN), implicitly converted to R
.
where INVOKE(f, t1, t2, ..., tN) is defined as follows:
f
is a pointer to member function of class T
: std::is_base_of<T, std::remove_reference_t<decltype(t1)>>::value
is true
, then INVOKE(f, t1, t2, ..., tN)
is equivalent to (t1.*f)(t2, ..., tN)
std::remove_cvref_t<decltype(t1)>
is a specialization of std::reference_wrapper
, then INVOKE(f, t1, t2, ..., tN)
is equivalent to (t1.get().*f)(t2, ..., tN)
(since C++17) t1
does not satisfy the previous items, then INVOKE(f, t1, t2, ..., tN)
is equivalent to ((*t1).*f)(t2, ..., tN)
. f
is a pointer to data member of class T
: std::is_base_of<T, std::remove_reference_t<decltype(t1)>>::value
is true
, then INVOKE(f, t1)
is equivalent to t1.*f
std::remove_cvref_t<decltype(t1)>
is a specialization of std::reference_wrapper
, then INVOKE(f, t1)
is equivalent to t1.get().*f
(since C++17) t1
does not satisfy the previous items, then INVOKE(f, t1)
is equivalent to (*t1).*f
INVOKE(f, t1, t2, ..., tN)
is equivalent to f(t1, t2, ..., tN)
(that is, f
is a FunctionObject) For pointers to member functions and pointers to data members, t1
may be a regular pointer or an object of class type that overloads operator*
, such as std::unique_ptr
or std::shared_ptr
.
Pointers to data members are Callable, even though no function calls take place.
In addition, the following standard library facilities accept any Callable type (not just FunctionObject).
std::function |
|
std::bind |
|
std::result_of |
|
std::thread::thread |
|
std::call_once |
|
std::async |
|
std::packaged_task |
|
std::reference_wrapper |
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2420 | C++11 | when R is void, the result must be implicitly convertible to void (which is impossible) | the result is explicitly converted to void when R is cv void |
(C++17) | checks if a type can be invoked (as if by std::invoke ) with the given argument types (class template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/named_req/Callable