Commit 1bf1654c authored by Marcelo Juchem's avatar Marcelo Juchem Committed by Jordan DeLong

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
parent e4f530f8
......@@ -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);
......
/*
* 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);
......
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