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 <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-10-11 15:38:22 +02:00
1 parent c10760455b
commit 170a32b27d
3 files changed
+13 -8

No files matched your search

@@ -920,7 +920,7 @@ class binary_writer
std::string result; std::string result;
do do
{ {
result.push_back(static_cast<char>('0' + n % 10)); result.push_back(static_cast<char>('0' + (n % 10)));
n = static_cast<NumberType>(n / 10); n = static_cast<NumberType>(n / 10);
} }
while (n != 0); while (n != 0);
+1 -1
View File
@@ -22655,7 +22655,7 @@ class binary_writer
std::string result; std::string result;
do do
{ {
result.push_back(static_cast<char>('0' + n % 10)); result.push_back(static_cast<char>('0' + (n % 10)));
n = static_cast<NumberType>(n / 10); n = static_cast<NumberType>(n / 10);
} }
while (n != 0); while (n != 0);
+11 -6
View File
@@ -34,12 +34,17 @@ class class_int
// trivial, like the 128-bit integer classes: the value is stored in a union // trivial, like the 128-bit integer classes: the value is stored in a union
class_int() = default; class_int() = default;
// implicit from built-in integers and explicit from floats, like absl::int128 // implicit from built-in integers and explicit from floats, like absl::int128;
template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0> // non-template overloads (as absl::int128 has) because clang 3.4 and 3.5
class_int(T v) : value(static_cast<std::int64_t>(v)) {} // NOLINT(google-explicit-constructor,hicpp-explicit-conversions) // reject basic_json's defaulted data() for a number type with template
// constructors
template<typename T, typename std::enable_if<std::is_floating_point<T>::value, int>::type = 0> class_int(int v) : value(v) {} // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
explicit class_int(T v) : value(static_cast<std::int64_t>(v)) {} 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<std::int64_t>(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<std::int64_t>(v)) {} // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
explicit class_int(double v) : value(static_cast<std::int64_t>(v)) {}
template<typename T, typename std::enable_if<std::is_arithmetic<T>::value, int>::type = 0> template<typename T, typename std::enable_if<std::is_arithmetic<T>::value, int>::type = 0>
explicit operator T() const explicit operator T() const