dimanche 31 janvier 2016

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

I've seen the operator== of a std::function misused more than once and I've had to explain what is its real use.
For the sake of clarity in favor of future readers, here is the documentation.
To sum up by citing the documentation above mentioned:

Compares a std::function with a null pointer. Empty functions (that is, functions without a callable target) compare equal, non-empty functions compare non-equal.

That said, the std::function has also the operator bool() (here the documentation) that works almost the same way and can be used as a replacement for the comparison my_func == nullptr.
In particular, it is said that:

Checks whether *this stores a callable function target, i.e. is not empty.

I cannot see a case where one can be used and the other does not fit well, so I cannot understand what is the purpose of the operator==, apart for the fact that it can be misunderstood and misused the most of the times.

Is there a particular case in which one cannot be used?
Otherwise, I'd rather invite my colleagues to use the bool operator and feel free to ignore the comparison operator, for it's so easy to be misused...

Aucun commentaire:

Enregistrer un commentaire