What is the real purpose of the == operator for the std :: function?

I saw that operator==from the std::functionabused more than once, and I had to explain what its real use.
For clarity, in favor of future readers here is the documentation.
The above documentation says that it:

Compares a std :: function with a null pointer. Empty functions (i.e. Functions without a called target) compare equal, non-empty functions compare unequal.

However, it std::functionalso has operator bool()( here is the documentation), which behaves almost the same and can be instead of a comparison my_func == nullptr.
In particular, it is said that he:

Checks whether * this object retains the objective function, i.e. not empty.

I don’t see a case where it can be used, and the other one does not fit well, so I can’t understand what the purpose is operator==, except that it can be misunderstood and used incorrectly once.

Is there a specific case when you can not use?
Are they interchangeable because they are actually the same, or does it make sense to have two different operators?

+4
source share
1 answer

The goal is simple:

Interface emulation of a simple function pointer, as well as reasonably possible.

Admittedly, I think they overdid it a bit with what's there ... even for use in templates.


nullptr , :

IIIb.

==,! =, <, > , <= >= .

: (in) .

: , - .

, , funter , :

.target_type() .target<T>().

, , , , .

+2

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


All Articles