Daily bit(e) of C++ | std::compare_strong_order_fallback, std::compare_weak_order_fallback, std::compare_partial_order_fallback
Daily bit(e) of C++ ♻️50, Bridging legacy code with code relying on C++20 three-way comparison.
The three-way comparison introduced in C++20 can significantly simplify the implementation and composition of comparison functions. However, the operator<=> will only work with types that implement a three-way comparison.
If implementing a three-way comparison isn't feasible, the calling code can instead use the helper functions std::compare_strong_order_fallback, std::compare_weak_order_fallback and std::compare_partial_order_fallback.
These helpers will work with operator<=> and the old-style comparison functions.
The strong and weak variants only require that the arguments are comparable using operator== and operator<. The partial variant also requires that the arguments are comparable using operator< when switched (i.e. a<b and b<a).