+/**
+ * A traits class to check for incomplete types.
+ *
+ * Example:
+ *
+ * struct FullyDeclared {}; // complete type
+ * struct ForwardDeclared; // incomplete type
+ *
+ * is_complete<int>::value // evaluates to true
+ * is_complete<FullyDeclared>::value // evaluates to true
+ * is_complete<ForwardDeclared>::value // evaluates to false
+ *
+ * struct ForwardDeclared {}; // declared, at last
+ *
+ * is_complete<ForwardDeclared>::value // now it evaluates to true
+ *
+ * @author: Marcelo Juchem <marcelo@fb.com>
+ */
+template <typename T>
+class is_complete {
+ template <unsigned long long> struct sfinae {};
+ template <typename U>
+ constexpr static bool test(sfinae<sizeof(U)>*) { return true; }
+ template <typename> constexpr static bool test(...) { return false; }
+public:
+ constexpr static bool value = test<T>(nullptr);
+};
+
+/*
+ * Complementary type traits for integral comparisons.
+ *
+ * For instance, `if(x < 0)` yields an error in clang for unsigned types
+ * when -Werror is used due to -Wtautological-compare
+ *
+ *
+ * @author: Marcelo Juchem <marcelo@fb.com>
+ */
+
+namespace detail {
+
+template <typename T, bool>
+struct is_negative_impl {
+ constexpr static bool check(T x) { return x < 0; }
+};
+
+template <typename T>
+struct is_negative_impl<T, false> {
+ constexpr static bool check(T x) { return false; }
+};
+
+template <typename RHS, RHS rhs, typename LHS>
+bool less_than_impl(
+ typename std::enable_if<
+ (rhs <= std::numeric_limits<LHS>::max()
+ && rhs > std::numeric_limits<LHS>::min()),
+ LHS
+ >::type const lhs
+) {
+ return lhs < rhs;
+}
+
+template <typename RHS, RHS rhs, typename LHS>
+bool less_than_impl(
+ typename std::enable_if<
+ (rhs > std::numeric_limits<LHS>::max()),
+ LHS
+ >::type const
+) {
+ return true;
+}
+
+template <typename RHS, RHS rhs, typename LHS>
+bool less_than_impl(
+ typename std::enable_if<
+ (rhs <= std::numeric_limits<LHS>::min()),
+ LHS
+ >::type const
+) {
+ return false;
+}
+
+template <typename RHS, RHS rhs, typename LHS>
+bool greater_than_impl(
+ typename std::enable_if<
+ (rhs <= std::numeric_limits<LHS>::max()
+ && rhs >= std::numeric_limits<LHS>::min()),
+ LHS
+ >::type const lhs
+) {
+ return lhs > rhs;
+}
+
+template <typename RHS, RHS rhs, typename LHS>
+bool greater_than_impl(
+ typename std::enable_if<
+ (rhs > std::numeric_limits<LHS>::max()),
+ LHS
+ >::type const
+) {
+ return false;
+}
+
+template <typename RHS, RHS rhs, typename LHS>
+bool greater_than_impl(
+ typename std::enable_if<
+ (rhs < std::numeric_limits<LHS>::min()),
+ LHS
+ >::type const
+) {
+ return true;
+}
+
+} // namespace detail {
+
+// same as `x < 0`
+template <typename T>
+constexpr bool is_negative(T x) {
+ return folly::detail::is_negative_impl<T, std::is_signed<T>::value>::check(x);
+}
+
+// same as `x <= 0`
+template <typename T>
+constexpr bool is_non_positive(T x) { return !x || folly::is_negative(x); }
+
+// same as `x > 0`
+template <typename T>
+constexpr bool is_positive(T x) { return !is_non_positive(x); }
+
+// same as `x >= 0`
+template <typename T>
+constexpr bool is_non_negative(T x) {
+ return !x || is_positive(x);
+}
+
+template <typename RHS, RHS rhs, typename LHS>
+bool less_than(LHS const lhs) {
+ return detail::less_than_impl<
+ RHS, rhs, typename std::remove_reference<LHS>::type
+ >(lhs);
+}
+
+template <typename RHS, RHS rhs, typename LHS>
+bool greater_than(LHS const lhs) {
+ return detail::greater_than_impl<
+ RHS, rhs, typename std::remove_reference<LHS>::type
+ >(lhs);
+}
+