Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

11
  • First, x == y does not mean (x <=> y) == 0. Second, given the C++20 expression rewrite symmetry, I don't think the "comparison operators shouldn't be member functions" is a real guideline. Members work just fine for nearly all use-cases. Third, why use std::is_eq(c) when you, everywhere else, use c == 0? Commented Apr 18, 2024 at 13:13
  • @Barry 1. fair point; even if == is provided as a result of defaulting three-way (which is why I've listed that as "meaning", it's not delegated to it. I'll see about rewording that part. 2. It's still the advice I'd give. While it matters less in C++20, it's still more teachable to have the same advice for C++17 and older, and not confuse readers with additional rules that only exist due to rewriting. == is meant to be symmetrical, so make it a free function; simple as. 3. Because in the tables, I'm referring to equivalences in the standard, not to what I'd consider most readable code. Commented Apr 18, 2024 at 13:31
  • 3. I don't see how you consider std::is_eq(c) to be more readable than c == 0 - especially when you consider that every example anywhere uses the latter formulation. Commented Apr 18, 2024 at 14:28
  • @Barry because it's self-documenting. Someone intricately familiar with the standard or who is the middle of reading this answer probably understands what comparing the result of a three-way to zero does. Someone less familiar with the subject matter will likely understand std::is_eq better. Also the alternative is writing !(c == 0) which someone unknowingly could "optimize" to c != 0. The alternative with std::is_eq is safer against such hasty uninformed cleanup attempts because it doesn't look intuitively wrong. Commented Apr 18, 2024 at 19:36
  • Not sure what you're talking about,!(c == 0) and c != 0 mean the same thing, by definition. Exhaustive demo. std::is_eq(c) is really just defined to mean c == 0, ditto std::is_neq(c) is just defined to be c != 0... Commented Apr 18, 2024 at 19:44