Initial Question:
Why is std::function (also applicable to boost::function and std::tr1::function) not equality comparable?
Answer:
Reason for Non-Comparability:
std::function is designed to accommodate a wide range of callable types. Imposing equality comparison on all callable types would be burdensome to implementers. Additionally, even if implemented, equality checks would likely be narrow, not accounting for equivalent functions constructed differently.
"Possible Hole in the Type System" Explanation:
Deleting the overloaded == and != operators completely prevents their use, ensuring that any attempt to compare functions implicitly will result in a compile-time error. This approach eliminates the possibility of unintended or problematic implicit conversions.
Comparison to std::shared_ptr:
Unlike std::function, std::shared_ptr has well-defined equality semantics. Two shared_ptr instances are considered equal if they are both empty or if they are both non-empty and refer to the same object.
The above is the detailed content of Why are `std::function`, `boost::function`, and `std::tr1::function` not Equality Comparable?. For more information, please follow other related articles on the PHP Chinese website!