I've been reading a bit about C++20's consistent comparison (i.e. operator<=>
) but couldn't understand what's the practical difference between std::strong_ordering
and std::weak_ordering
(same goes for the _equality
version for this manner).
Other than being very descriptive about the substitutability of the type, does it actually affect the generated code? Does it add any constraints for how one could use the type?
Would love to see a real-life example that demonstrates this.
Does it add any constraints for how one could use the type?
One very significant constraint (which wasn't intended by the original paper) was the adoption of the significance of This is no longer the case, as non-type template parameters no longer work this way (see P1907R0 for explanation of issues and P1907R1 for wording of the new rules).strong_ordering
by P0732 as an indicator that a class type can be used as a non-type template parameter. weak_ordering
isn't sufficient for this case due to how template equivalence has to work.
Generally, it's possible that some algorithms simply require weak_ordering
but other algorithms require strong_ordering
, so being able to annotate that on the type might mean a compile error (insufficiently strong ordering provided) instead of simply failing to meet the algorithm's requirements at runtime and hence just being undefined behavior. But all the algorithms in the standard library and the Ranges TS that I know of simply require weak_ordering
. I do not know of one that requires strong_ordering
off the top of my head.
Does it actually affect the generated code?
Outside of the cases where strong_ordering
is required, or an algorithm explicitly chooses different behavior based on the comparison category, no.