[cfe-dev] Spaceship operator oddity with optional
Nuno Lopes via cfe-dev
cfe-dev at lists.llvm.org
Tue Jan 26 05:58:39 PST 2021
Hi,
I just bumped into this mysterious behavior with the spaceship operator over
optional:
struct expr {
std::strong_ordering operator<=>(const expr &rhs) const;
//bool operator==(const expr &rhs) const;
};
int f() {
return std::is_eq(std::optional<expr>() <=> std::optional<expr>());
//return std::optional<expr>() == std::optional<expr>();
}
Function f() returns 0 both on gcc & clang trunk
(https://gcc.godbolt.org/z/fv85eP).
However, if you uncomment the operator==() line, the function starts
returning 1.
The documentation of
https://en.cppreference.com/w/cpp/utility/optional/operator_cmp just says:
7) If bool(lhs) && bool(rhs) is true returns *x <=> *y
Otherwise, returns bool(lhs) <=> bool(rhs)
So no word about operator=='s existence.
Is this a bug in both clang & gcc or just some unobvious 3-way comparison
rule I'm not aware?
Thanks,
Nuno
More information about the cfe-dev
mailing list