What is the "actual" function of std ::?

Here:

http://en.cppreference.com/w/cpp/utility/functional/function

operator bool : "Checks if the stored called object is valid."

Presumably the default built std::function not valid, but is this the only case?

Also, how does he check if this is true?

Is the case when operator() calls std::bad_function_call exactly the case when the object is invalid?

+6
source share
2 answers

It is poorly written as it is, your confusion is justified. By "valid" they mean "has a purpose."

A std::function "has a purpose" when a function is assigned to it:

 std::function<void()> x; // no target std::function<void()> y = some_void_function; // has target x = some_other_void_function; // has target y = nullptr; // no target x = y; // no target 

They had to either define "valid" before using them, or simply stick to the official wording.

+7
source

The language standard says:

explicit operator bool() const noexcept;

Returns: true if * it has a target, otherwise false.

The value of function has anything. By default, the constructed function obviously does not work.

+1
source

Source: https://habr.com/ru/post/922483/


All Articles