lundi 18 avril 2022

How to understand the `callable` in the right way?

As per the document, which says:

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)

Requirements

The type T satisfies Callable if Given f, an object of type T ArgTypes, suitable list of argument types R,

expressions must be valid:

std::declval()...) the expression is well-formed in unevaluated contextExpression Requirements INVOKE(f,suitable return type The following

Question 1:

If I understand correctly, given void foo(int) {}, std::function(foo) is not callable, whereas std::function(foo, 1) is callable. Am I right?

Question 2: What confuses me is the statement below.

As per the document, which says[emphasis mine]:

The class template std::packaged_task wraps any Callable target (function, lambda expression, bind expression, or another function object) so that it can be invoked asynchronously. Its return value or exception thrown is stored in a shared state which can be accessed through std::future objects.

As you see that the class template std::packaged_task wraps any Callable target, and std::function{foo} is not callable, but std::packged_task<void(int)> task{f}; compiles.

Aucun commentaire:

Enregistrer un commentaire