Home  >  Article  >  Backend Development  >  Why Can\'t I Compare `std::function` Objects for Equality?

Why Can\'t I Compare `std::function` Objects for Equality?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-30 15:55:03827browse

Why Can't I Compare `std::function` Objects for Equality?

The Enigma Behind std::function's Non-Equality Comparability

std::function encapsulates arbitrary callable objects, presenting a unique challenge for equality comparison. Implementing such comparisons would necessitate equality comparability for all callable types, which would impose a significant burden on implementors. Moreover, equal functionality could yield non-equal comparisons based on differing argument binding orders. Therefore, the impossible task of guaranteeing equivalence in all cases prohibits equality comparability.

The "possible hole in the type system" mentioned in the C 11 draft likely refers to the inherent ambiguity in implicit conversions. Deleting equality operators and coercing explicit use ensures the absence of valid code generated from their invocation.

Unlike std::function, std::shared_ptr possesses a clearly defined equality semantics. Two pointers are equal precisely when both are empty or both refer to the same object. This well-defined behavior allows for straightforward and meaningful equality comparisons.

The above is the detailed content of Why Can\'t I Compare `std::function` Objects for Equality?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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