From bfb0b6f48db15e2e6d7c40edae7c8b4bc95c07fb Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Sat, 15 Oct 2022 14:08:01 -0700 Subject: [PATCH] [ADT] Simplify getAsInteger and consumeInteger (NFC) This patch replaces the std::enable_if_t trick with a "constexpr if" so that the resulting code looks more like "normal" C++ code. Differential Revision: https://reviews.llvm.org/D136024 --- llvm/include/llvm/ADT/StringRef.h | 70 +++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 40 deletions(-) diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h index 25dcda0..02159eb 100644 --- a/llvm/include/llvm/ADT/StringRef.h +++ b/llvm/include/llvm/ADT/StringRef.h @@ -458,28 +458,23 @@ namespace llvm { /// If the string is invalid or if only a subset of the string is valid, /// this returns true to signify the error. The string is considered /// erroneous if empty or if it overflows T. - template - std::enable_if_t::is_signed, bool> - getAsInteger(unsigned Radix, T &Result) const { - long long LLVal; - if (getAsSignedInteger(*this, Radix, LLVal) || + template bool getAsInteger(unsigned Radix, T &Result) const { + if constexpr (std::numeric_limits::is_signed) { + long long LLVal; + if (getAsSignedInteger(*this, Radix, LLVal) || static_cast(LLVal) != LLVal) - return true; - Result = LLVal; - return false; - } - - template - std::enable_if_t::is_signed, bool> - getAsInteger(unsigned Radix, T &Result) const { - unsigned long long ULLVal; - // The additional cast to unsigned long long is required to avoid the - // Visual C++ warning C4805: '!=' : unsafe mix of type 'bool' and type - // 'unsigned __int64' when instantiating getAsInteger with T = bool. - if (getAsUnsignedInteger(*this, Radix, ULLVal) || - static_cast(static_cast(ULLVal)) != ULLVal) - return true; - Result = ULLVal; + return true; + Result = LLVal; + } else { + unsigned long long ULLVal; + // The additional cast to unsigned long long is required to avoid the + // Visual C++ warning C4805: '!=' : unsafe mix of type 'bool' and type + // 'unsigned __int64' when instantiating getAsInteger with T = bool. + if (getAsUnsignedInteger(*this, Radix, ULLVal) || + static_cast(static_cast(ULLVal)) != ULLVal) + return true; + Result = ULLVal; + } return false; } @@ -492,25 +487,20 @@ namespace llvm { /// erroneous if empty or if it overflows T. /// The portion of the string representing the discovered numeric value /// is removed from the beginning of the string. - template - std::enable_if_t::is_signed, bool> - consumeInteger(unsigned Radix, T &Result) { - long long LLVal; - if (consumeSignedInteger(*this, Radix, LLVal) || - static_cast(static_cast(LLVal)) != LLVal) - return true; - Result = LLVal; - return false; - } - - template - std::enable_if_t::is_signed, bool> - consumeInteger(unsigned Radix, T &Result) { - unsigned long long ULLVal; - if (consumeUnsignedInteger(*this, Radix, ULLVal) || - static_cast(static_cast(ULLVal)) != ULLVal) - return true; - Result = ULLVal; + template bool consumeInteger(unsigned Radix, T &Result) { + if constexpr (std::numeric_limits::is_signed) { + long long LLVal; + if (consumeSignedInteger(*this, Radix, LLVal) || + static_cast(static_cast(LLVal)) != LLVal) + return true; + Result = LLVal; + } else { + unsigned long long ULLVal; + if (consumeUnsignedInteger(*this, Radix, ULLVal) || + static_cast(static_cast(ULLVal)) != ULLVal) + return true; + Result = ULLVal; + } return false; } -- 2.7.4