From 170a32b27d451ad45676ed6c603b82d0dc3cfaa2 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 11 Oct 2026 15:38:22 +0200 Subject: [PATCH] Fix clang-tidy and clang 3.4/3.5 failures in the custom number type changes Parenthesize n % 10 in integer_to_string for readability-math-missing-parentheses, and give class_int non-template constructors (as absl::int128 has): clang 3.4 and 3.5 reject the defaulted basic_json::data() constructor for a number type with template constructors. Signed-off-by: Niels Lohmann --- .../nlohmann/detail/output/binary_writer.hpp | 2 +- single_include/nlohmann/json.hpp | 2 +- tests/src/unit-custom-number-types.cpp | 17 +++++++++++------ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index 3ab316c10..817998c05 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -920,7 +920,7 @@ class binary_writer std::string result; do { - result.push_back(static_cast('0' + n % 10)); + result.push_back(static_cast('0' + (n % 10))); n = static_cast(n / 10); } while (n != 0); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index f26b6683f..cd7269529 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -22655,7 +22655,7 @@ class binary_writer std::string result; do { - result.push_back(static_cast('0' + n % 10)); + result.push_back(static_cast('0' + (n % 10))); n = static_cast(n / 10); } while (n != 0); diff --git a/tests/src/unit-custom-number-types.cpp b/tests/src/unit-custom-number-types.cpp index 787e36557..f9c197574 100644 --- a/tests/src/unit-custom-number-types.cpp +++ b/tests/src/unit-custom-number-types.cpp @@ -34,12 +34,17 @@ class class_int // trivial, like the 128-bit integer classes: the value is stored in a union class_int() = default; - // implicit from built-in integers and explicit from floats, like absl::int128 - template::value, int>::type = 0> - class_int(T v) : value(static_cast(v)) {} // NOLINT(google-explicit-constructor,hicpp-explicit-conversions) - - template::value, int>::type = 0> - explicit class_int(T v) : value(static_cast(v)) {} + // implicit from built-in integers and explicit from floats, like absl::int128; + // non-template overloads (as absl::int128 has) because clang 3.4 and 3.5 + // reject basic_json's defaulted data() for a number type with template + // constructors + class_int(int v) : value(v) {} // NOLINT(google-explicit-constructor,hicpp-explicit-conversions) + class_int(unsigned v) : value(v) {} // NOLINT(google-explicit-constructor,hicpp-explicit-conversions) + class_int(long v) : value(v) {} // NOLINT(google-explicit-constructor,hicpp-explicit-conversions) + class_int(unsigned long v) : value(static_cast(v)) {} // NOLINT(google-explicit-constructor,hicpp-explicit-conversions) + class_int(long long v) : value(v) {} // NOLINT(google-explicit-constructor,hicpp-explicit-conversions) + class_int(unsigned long long v) : value(static_cast(v)) {} // NOLINT(google-explicit-constructor,hicpp-explicit-conversions) + explicit class_int(double v) : value(static_cast(v)) {} template::value, int>::type = 0> explicit operator T() const