Commit 87efa7ee authored by Eric Niebler's avatar Eric Niebler Committed by Facebook Github Bot

Reformulate constexpr_min and constexpr_max to achieve stability in sorting as...

Reformulate constexpr_min and constexpr_max to achieve stability in sorting as described in http://stepanovpapers.com/notes.pdf

Summary: There is a famous long-standing "bug" in the standard library regarding the semantics of min and max wrt values that are equivalent wrt op< but not equal. Let's not make the same mistake with constexpr_min and constexpr_max.

Reviewed By: yfeldblum, luciang, Orvid, ot

Differential Revision: D4269635

fbshipit-source-id: 19b464c949dc0cf07afb08eaf657ae8b242ca42d
parent fae84c77
......@@ -22,14 +22,20 @@
namespace folly {
// TLDR: Prefer using operator< for ordering. And when
// a and b are equivalent objects, we return b to make
// sorting stable.
// See http://stepanovpapers.com/notes.pdf for details.
template <typename T>
constexpr T constexpr_max(T a, T b) {
return a > b ? a : b;
return b < a ? a : b;
}
// When a and b are equivalent objects, we return a to
// make sorting stable.
template <typename T>
constexpr T constexpr_min(T a, T b) {
return a < b ? a : b;
return b < a ? b : a;
}
namespace detail {
......@@ -67,7 +73,7 @@ struct constexpr_abs_helper<
return t < static_cast<T>(0) ? -t : t;
}
};
}
} // namespace detail
template <typename T>
constexpr auto constexpr_abs(T t)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment