Home > Backend Development > C++ > body text

Why are `std::function`, `boost::function`, and `std::tr1::function` not Equality Comparable?

Linda Hamilton
Release: 2024-11-01 19:26:02
Original
111 people have browsed it

Why are `std::function`, `boost::function`, and `std::tr1::function` not Equality Comparable?

Why std::function is Not Equality Comparable

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!