From 64313e915c45a2a37045e7b65015e557112dc4b6 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 30 Aug 2024 10:39:03 -0700 Subject: [PATCH] Move redundant initialization to compile time --- include/fmt/format.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index c29ff9a0..adad650c 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1338,9 +1338,10 @@ FMT_CONSTEXPR auto format_decimal(OutputIt out, UInt value, int num_digits) return out; } // Buffer is large enough to hold all digits (digits10 + 1). - char buffer[digits10() + 1] = {}; + char buffer[digits10() + 1]; + if (is_constant_evaluated()) fill_n(buffer, sizeof(buffer), '\0'); do_format_decimal(buffer, value, num_digits); - return detail::copy_noinline(buffer, buffer + num_digits, out); + return copy_noinline(buffer, buffer + num_digits, out); } template @@ -1374,7 +1375,8 @@ FMT_CONSTEXPR inline auto format_base2e(int base_bits, OutputIt out, UInt value, return out; } // Make buffer large enough for any base. - char buffer[num_bits()] = {}; + char buffer[num_bits()]; + if (is_constant_evaluated()) fill_n(buffer, sizeof(buffer), '\0'); format_base2e(base_bits, buffer, value, num_digits, upper); return detail::copy_noinline(buffer, buffer + num_digits, out); } @@ -2168,7 +2170,8 @@ FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, write_int_arg arg, static_assert(std::is_same>::value, ""); constexpr int buffer_size = num_bits(); - char buffer[buffer_size] = {}; + char buffer[buffer_size]; + if (is_constant_evaluated()) fill_n(buffer, buffer_size, '\0'); const char* begin = nullptr; const char* end = buffer + buffer_size;