Przejdź do głównej zawartości

C++ named requirements: Callable

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) are applicable.

INVOKE can be performed explicitly using the library function std::invoke. (od C++17)
INVOKE<R> can be performed explicitly using the library function std::invoke_r. (od C++23)

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:

pubExpressionRequirements
pub
INVOKE<R>(f,std::declval<ArgTypes>()...)
the expression is well-formed in unevaluated context

Notes​

Pointers to data members are Callable, even though no function calls take place.

Standard library​

In addition, the following standard library facilities accept any Callable type (not just FunctionObject):

pubfunction(C++11)wraps callable object of any copy constructible type with specified function call signature
pubmove_only_function(C++23)wraps callable object of any type with specified function call signature
pubbind(C++11)binds one or more arguments to a function object
pubbind_front(C++20)
bind_back(C++23)
bind a variable number of arguments, in order, to a function object
pubreference_wrapper(C++11)CopyConstructible and CopyAssignable reference wrapper
pubresult_of(C++11) (removed in C++20)
invoke_result(C++17)
deduces the result type of invoking a callable object with a set of arguments
pubthread(C++11)manages a separate thread
pubjthread(C++20)std::thread with support for auto-joining and cancellation
pubcall_once(C++11)invokes a function only once even if called from multiple threads
pubasync(C++11)runs a function asynchronously (potentially in a new thread) and returns a std::future that will hold the result
pubpackaged_task(C++11)packages a function to store its return value for asynchronous retrieval

C++ named requirements: Callable

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) are applicable.

INVOKE can be performed explicitly using the library function std::invoke. (od C++17)
INVOKE<R> can be performed explicitly using the library function std::invoke_r. (od C++23)

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:

pubExpressionRequirements
pub
INVOKE<R>(f,std::declval<ArgTypes>()...)
the expression is well-formed in unevaluated context

Notes​

Pointers to data members are Callable, even though no function calls take place.

Standard library​

In addition, the following standard library facilities accept any Callable type (not just FunctionObject):

pubfunction(C++11)wraps callable object of any copy constructible type with specified function call signature
pubmove_only_function(C++23)wraps callable object of any type with specified function call signature
pubbind(C++11)binds one or more arguments to a function object
pubbind_front(C++20)
bind_back(C++23)
bind a variable number of arguments, in order, to a function object
pubreference_wrapper(C++11)CopyConstructible and CopyAssignable reference wrapper
pubresult_of(C++11) (removed in C++20)
invoke_result(C++17)
deduces the result type of invoking a callable object with a set of arguments
pubthread(C++11)manages a separate thread
pubjthread(C++20)std::thread with support for auto-joining and cancellation
pubcall_once(C++11)invokes a function only once even if called from multiple threads
pubasync(C++11)runs a function asynchronously (potentially in a new thread) and returns a std::future that will hold the result
pubpackaged_task(C++11)packages a function to store its return value for asynchronous retrieval