};
/*
- * Complementary type traits to check for a negative value.
+ * Complementary type traits to check for a negative/non-positive value.
*
- * if(x < 0) yields an error in clang for unsigned types when -Werror is used
+ * `if(x < 0)` yields an error in clang for unsigned types when -Werror is used
*/
namespace detail {
} // 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); }
+
} // namespace folly
FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(std::basic_string);
/*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2013 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
EXPECT_FALSE(IsZeroInitializable<vector<int>>::value);
}
+TEST(Traits, is_negative) {
+ EXPECT_TRUE(folly::is_negative(-1));
+ EXPECT_FALSE(folly::is_negative(0));
+ EXPECT_FALSE(folly::is_negative(1));
+ EXPECT_FALSE(folly::is_negative(0u));
+ EXPECT_FALSE(folly::is_negative(1u));
+
+ EXPECT_TRUE(folly::is_non_positive(-1));
+ EXPECT_TRUE(folly::is_non_positive(0));
+ EXPECT_FALSE(folly::is_non_positive(1));
+ EXPECT_TRUE(folly::is_non_positive(0u));
+ EXPECT_FALSE(folly::is_non_positive(1u));
+}
+
int main(int argc, char ** argv) {
testing::InitGoogleTest(&argc, argv);
google::ParseCommandLineFlags(&argc, &argv, true);