Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adding is_non_positive traits

Summary:
template <typename SomeInt>
void foo(SomeInt x) {
// yields an error in clang when SomeInt is unsigned and -Werror is used
if(x <= 0) {
//...
}
}

Test Plan: added unit tests

Reviewed By: andrei.alexandrescu@fb.com

FB internal diff: D735735
  • Loading branch information...
commit 1bf1654c689b31ac7b53f499b8f1810548fbda7a 1 parent e4f530f
@juchem juchem authored jdelong committed
Showing with 22 additions and 3 deletions.
  1. +7 −2 folly/Traits.h
  2. +15 −1 folly/test/TraitsTest.cpp
View
9 folly/Traits.h
@@ -278,9 +278,9 @@ struct IsOneOf<T, T1, Ts...> {
};
/*
- * 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 {
@@ -297,11 +297,16 @@ struct is_negative_impl<T, false> {
} // 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);
View
16 folly/test/TraitsTest.cpp
@@ -1,5 +1,5 @@
/*
- * 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.
@@ -82,6 +82,20 @@ TEST(Traits, bitAndInit) {
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);
Please sign in to comment.
Something went wrong with that request. Please try again.