From 3da6d3dedd28be91b7b297ffaaff8933c227feab Mon Sep 17 00:00:00 2001 From: Michael Winterberg Date: Thu, 30 Sep 2021 11:27:31 -0700 Subject: [PATCH] Made basic_string_view's const Char* constructor constexpr in C++17. Also added commentary for why std::strlen is directly called in certain situations. Addresses https://github.com/fmtlib/fmt/issues/2455#issuecomment-931346096. --- include/fmt/core.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 2bac1cb3..a8402303 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -422,11 +422,17 @@ template class basic_string_view { */ FMT_CONSTEXPR_CHAR_TRAITS FMT_INLINE - basic_string_view(const Char* s) : data_(s) { + basic_string_view(const Char* s) : data_(s), size_(0) { + // This test is needed to ensure that this constructor is constexpr in C++17 + // modes. +#ifdef __cpp_lib_is_constant_evaluated if (detail::const_check(std::is_same::value && - !detail::is_constant_evaluated())) + !detail::is_constant_evaluated())) { + // Call strlen directly for better code generation in non-optimized + // builds. size_ = std::strlen(reinterpret_cast(s)); - else + } else +#endif size_ = std::char_traits::length(s); }