From dd9cce3051a5bed81e549e8ef39deb4aee23db8d Mon Sep 17 00:00:00 2001 From: Eric Niebler Date: Mon, 5 Dec 2016 13:06:16 -0800 Subject: [PATCH] constexpr_strlen works for types other than char. Remove bogus check for nullptr. Summary: overload constexpr_strlen to work with other character types Reviewed By: yfeldblum, luciang, Orvid, ot Differential Revision: D4269654 fbshipit-source-id: 2fcd666669efb51d77e847ff31bda3a301e763da --- folly/portability/Constexpr.h | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/folly/portability/Constexpr.h b/folly/portability/Constexpr.h index dc21a7d3..0827115e 100755 --- a/folly/portability/Constexpr.h +++ b/folly/portability/Constexpr.h @@ -81,19 +81,27 @@ constexpr auto constexpr_abs(T t) return detail::constexpr_abs_helper::go(t); } -#ifdef _MSC_VER -constexpr size_t constexpr_strlen_internal(const char* s, size_t len) { - return *s == '\0' ? len : constexpr_strlen_internal(s + 1, len + 1); +namespace detail { + +template +constexpr size_t constexpr_strlen_internal(const Char* s, size_t len) { + return *s == Char(0) ? len : constexpr_strlen_internal(s + 1, len + 1); } static_assert(constexpr_strlen_internal("123456789", 0) == 9, "Someone appears to have broken constexpr_strlen..."); -#endif +} // namespace detail + +template +constexpr size_t constexpr_strlen(const Char* s) { + return detail::constexpr_strlen_internal(s, 0); +} +template <> constexpr size_t constexpr_strlen(const char* s) { #if defined(__clang__) return __builtin_strlen(s); #elif defined(_MSC_VER) - return s == nullptr ? 0 : constexpr_strlen_internal(s, 0); + return detail::constexpr_strlen_internal(s, 0); #else return std::strlen(s); #endif -- 2.34.1