diff options
author | Benjamin Barenblat <bbaren@google.com> | 2023-09-07 13:16:09 -0400 |
---|---|---|
committer | Benjamin Barenblat <bbaren@google.com> | 2023-09-07 13:16:09 -0400 |
commit | 6fdbff8bbce2a1debdc060df381f39e3dcfb65af (patch) | |
tree | 71f1ef38477a65d5cce472fc042c90087c2bb351 /absl/types/compare.h | |
parent | 8d4a80fe37176b1170d7dce0772dea9584ec3e32 (diff) | |
parent | 29bf8085f3bf17b84d30e34b3d7ff8248fda404e (diff) |
Merge new upstream LTS 20230802.0
Diffstat (limited to 'absl/types/compare.h')
-rw-r--r-- | absl/types/compare.h | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/absl/types/compare.h b/absl/types/compare.h index 1a965e97..2b89b69c 100644 --- a/absl/types/compare.h +++ b/absl/types/compare.h @@ -36,6 +36,7 @@ #include <type_traits> #include "absl/base/attributes.h" +#include "absl/base/macros.h" #include "absl/meta/type_traits.h" namespace absl { @@ -45,14 +46,23 @@ namespace compare_internal { using value_type = int8_t; class OnlyLiteralZero { - // A private type which cannot be named to explicitly cast to it. - struct MatchLiteralZero; - public: +#if ABSL_HAVE_ATTRIBUTE(enable_if) + // On clang, we can avoid triggering modernize-use-nullptr by only enabling + // this overload when the value is a compile time integer constant equal to 0. + // + // In c++20, this could be a static_assert in a consteval function. + constexpr OnlyLiteralZero(int n) // NOLINT + __attribute__((enable_if(n == 0, "Only literal `0` is allowed."))) {} +#else // ABSL_HAVE_ATTRIBUTE(enable_if) // Accept only literal zero since it can be implicitly converted to a pointer - // type. nullptr constants will be caught by the other constructor which - // accepts a nullptr_t. - constexpr OnlyLiteralZero(MatchLiteralZero *) noexcept {} // NOLINT + // to member type. nullptr constants will be caught by the other constructor + // which accepts a nullptr_t. + // + // This constructor is not used for clang since it triggers + // modernize-use-nullptr. + constexpr OnlyLiteralZero(int OnlyLiteralZero::*) noexcept {} // NOLINT +#endif // Fails compilation when `nullptr` or integral type arguments other than // `int` are passed. This constructor doesn't accept `int` because literal `0` |