From 3aa3d879834e84ed6890f4b5c07c76cb810a4aa9 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 14 Oct 2026 19:34:12 +0200 Subject: [PATCH] Use std::array for the integer size class table clang-tidy (cppcoreguidelines-avoid-c-arrays) rejects the C-style array. Signed-off-by: Niels Lohmann --- include/nlohmann/detail/output/binary_writer.hpp | 14 ++++++++------ single_include/nlohmann/json.hpp | 14 ++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index b7f0ab5b6..de18100db 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -1897,15 +1897,17 @@ class binary_writer */ static unsigned integer_size_class(const std::uint64_t x) noexcept { - static constexpr std::uint8_t size_class_by_leading_zeros[64] = + static constexpr std::array size_class_by_leading_zeros = { - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 2, 2, 2, 2, 2, 2, 2, 2, - 1, 1, 1, 1, 1, 1, 1, 1 + { + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 2, 2, 2, 2, 2, 2, 2, 2, + 1, 1, 1, 1, 1, 1, 1, 1 + } }; // x | 1 keeps count_leading_zeros() away from 0 - return size_class_by_leading_zeros[count_leading_zeros(x | 1u)]; + return size_class_by_leading_zeros[static_cast(count_leading_zeros(x | 1u))]; } /*! diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 6ffaedb19..90cf3c66d 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -23929,15 +23929,17 @@ class binary_writer */ static unsigned integer_size_class(const std::uint64_t x) noexcept { - static constexpr std::uint8_t size_class_by_leading_zeros[64] = + static constexpr std::array size_class_by_leading_zeros = { - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 2, 2, 2, 2, 2, 2, 2, 2, - 1, 1, 1, 1, 1, 1, 1, 1 + { + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 2, 2, 2, 2, 2, 2, 2, 2, + 1, 1, 1, 1, 1, 1, 1, 1 + } }; // x | 1 keeps count_leading_zeros() away from 0 - return size_class_by_leading_zeros[count_leading_zeros(x | 1u)]; + return size_class_by_leading_zeros[static_cast(count_leading_zeros(x | 1u))]; } /*!