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
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 {
return t < static_cast<T>(0) ? -t : t;
}
};
-}
+} // namespace detail
template <typename T>
constexpr auto constexpr_abs(T t)