+/*
+ * 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) { return false; }
+};
+
+// folly::to integral specializations can end up generating code
+// inside what are really static ifs (not executed because of the templated
+// types) that violate -Wsign-compare and/or -Wbool-compare so suppress them
+// in order to not prevent all calling code from using it.
+FOLLY_PUSH_WARNING
+FOLLY_GCC_DISABLE_WARNING("-Wsign-compare")
+#if __GNUC_PREREQ(5, 0)
+FOLLY_GCC_DISABLE_WARNING("-Wbool-compare")
+#endif
+FOLLY_MSVC_DISABLE_WARNING(4388) // sign-compare
+FOLLY_MSVC_DISABLE_WARNING(4804) // bool-compare
+
+template <typename RHS, RHS rhs, typename LHS>
+bool less_than_impl(LHS const lhs) {
+ return
+ rhs > std::numeric_limits<LHS>::max() ? true :
+ rhs <= std::numeric_limits<LHS>::min() ? false :
+ lhs < rhs;
+}
+
+template <typename RHS, RHS rhs, typename LHS>
+bool greater_than_impl(LHS const lhs) {
+ return
+ rhs > std::numeric_limits<LHS>::max() ? false :
+ rhs < std::numeric_limits<LHS>::min() ? true :
+ lhs > rhs;
+}
+
+FOLLY_POP_WARNING
+
+} // 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);
+}
+
+namespace traits_detail {
+struct InPlaceTag {};
+template <class>
+struct InPlaceTypeTag {};
+template <std::size_t>
+struct InPlaceIndexTag {};
+}
+
+/**
+ * Like std::piecewise_construct, a tag type & instance used for in-place
+ * construction of non-movable contained types, e.g. by Synchronized.
+ * Follows the naming and design of std::in_place suggested in
+ * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0032r2.pdf
+ */
+using in_place_t = traits_detail::InPlaceTag (&)(traits_detail::InPlaceTag);
+
+template <class T>
+using in_place_type_t =
+ traits_detail::InPlaceTypeTag<T> (&)(traits_detail::InPlaceTypeTag<T>);
+
+template <std::size_t I>
+using in_place_index_t =
+ traits_detail::InPlaceIndexTag<I> (&)(traits_detail::InPlaceIndexTag<I>);
+
+inline traits_detail::InPlaceTag in_place(traits_detail::InPlaceTag = {}) {
+ return {};
+}
+
+template <class T>
+inline traits_detail::InPlaceTypeTag<T> in_place(
+ traits_detail::InPlaceTypeTag<T> = {}) {
+ return {};
+}
+
+template <std::size_t I>
+inline traits_detail::InPlaceIndexTag<I> in_place(
+ traits_detail::InPlaceIndexTag<I> = {}) {
+ return {};
+}
+
+// For backwards compatibility:
+using construct_in_place_t = in_place_t;
+
+inline traits_detail::InPlaceTag construct_in_place(
+ traits_detail::InPlaceTag = {}) {
+ return {};
+}
+
+/**
+ * Initializer lists are a powerful compile time syntax introduced in C++11
+ * but due to their often conflicting syntax they are not used by APIs for
+ * construction.
+ *
+ * Further standard conforming compilers *strongly* favor an
+ * std::initalizer_list overload for construction if one exists. The
+ * following is a simple tag used to disambiguate construction with
+ * initializer lists and regular uniform initialization.
+ *
+ * For example consider the following case
+ *
+ * class Something {
+ * public:
+ * explicit Something(int);
+ * Something(std::intiializer_list<int>);
+ *
+ * operator int();
+ * };
+ *
+ * ...
+ * Something something{1}; // SURPRISE!!
+ *
+ * The last call to instantiate the Something object will go to the
+ * initializer_list overload. Which may be surprising to users.
+ *
+ * If however this tag was used to disambiguate such construction it would be
+ * easy for users to see which construction overload their code was referring
+ * to. For example
+ *
+ * class Something {
+ * public:
+ * explicit Something(int);
+ * Something(folly::initlist_construct_t, std::initializer_list<int>);
+ *
+ * operator int();
+ * };
+ *
+ * ...
+ * Something something_one{1}; // not the initializer_list overload
+ * Something something_two{folly::initlist_construct, {1}}; // correct
+ */
+struct initlist_construct_t {};
+constexpr initlist_construct_t initlist_construct{};