Commit 66405fc6 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Function objects for performing constexpr comparisons

Summary:
[Folly] Function objects for performing `constexpr` comparisons for C++11 or gcc49.

After upgrading to C++14 and dropping support for C++11, these may be removed because the corresponding function objects in the C++14 standard library are `constexpr`-invocable.

Reviewed By: Orvid

Differential Revision: D7590351

fbshipit-source-id: a8702115a66836aff25761f374b8a2d7dbe12074
parent e64700bd
...@@ -22,6 +22,44 @@ ...@@ -22,6 +22,44 @@
namespace folly { namespace folly {
// TODO: Replace with std::equal_to, etc., after upgrading to C++14.
template <typename T>
struct constexpr_equal_to {
constexpr bool operator()(T const& a, T const& b) const {
return a == b;
}
};
template <typename T>
struct constexpr_not_equal_to {
constexpr bool operator()(T const& a, T const& b) const {
return a != b;
}
};
template <typename T>
struct constexpr_less {
constexpr bool operator()(T const& a, T const& b) const {
return a < b;
}
};
template <typename T>
struct constexpr_less_qual {
constexpr bool operator()(T const& a, T const& b) const {
return a <= b;
}
};
template <typename T>
struct constexpr_greater {
constexpr bool operator()(T const& a, T const& b) const {
return a > b;
}
};
template <typename T>
struct constexpr_reater_equal {
constexpr bool operator()(T const& a, T const& b) const {
return a >= b;
}
};
// TLDR: Prefer using operator< for ordering. And when // TLDR: Prefer using operator< for ordering. And when
// a and b are equivalent objects, we return b to make // a and b are equivalent objects, we return b to make
// sorting stable. // sorting stable.
...@@ -51,21 +89,9 @@ constexpr T const& ...@@ -51,21 +89,9 @@ constexpr T const&
constexpr_clamp(T const& v, T const& lo, T const& hi, Less less) { constexpr_clamp(T const& v, T const& lo, T const& hi, Less less) {
return less(v, lo) ? lo : less(hi, v) ? hi : v; return less(v, lo) ? lo : less(hi, v) ? hi : v;
} }
namespace detail {
// This can be replaced with std::less once everything we care about
// supports it.
template <typename T>
struct Less {
constexpr bool operator()(T const& a, T const& b) const {
return a < b;
}
};
} // namespace detail
template <typename T> template <typename T>
constexpr T const& constexpr_clamp(T const& v, T const& lo, T const& hi) { constexpr T const& constexpr_clamp(T const& v, T const& lo, T const& hi) {
return constexpr_clamp(v, lo, hi, detail::Less<T>{}); return constexpr_clamp(v, lo, hi, constexpr_less<T>{});
} }
namespace detail { namespace detail {
......
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