diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index dfbe54329a..3768f97fd3 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -664,6 +664,20 @@ class serializer } } + template ::value, int> = 0> + bool is_negative_number(NumberType x) + { + return x < 0; + } + + template < typename NumberType, + enable_if_t < std::is_unsigned::value, int > = 0 > + bool is_negative_number(NumberType /*unused*/) + { + return false; + } + /*! @brief dump an integer @@ -706,12 +720,11 @@ class serializer // use a pointer to fill the buffer auto buffer_ptr = number_buffer.begin(); // NOLINT(llvm-qualified-auto,readability-qualified-auto,cppcoreguidelines-pro-type-vararg,hicpp-vararg) - const bool is_negative = std::is_same::value && !(x >= 0); // see issue #755 number_unsigned_t abs_value; unsigned int n_chars{}; - if (is_negative) + if (is_negative_number(x)) { *buffer_ptr = '-'; abs_value = remove_sign(static_cast(x)); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 2716e1e7b3..037533e1a0 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -16607,6 +16607,20 @@ class serializer } } + template ::value, int> = 0> + bool is_negative_number(NumberType x) + { + return x < 0; + } + + template < typename NumberType, + enable_if_t < std::is_unsigned::value, int > = 0 > + bool is_negative_number(NumberType /*unused*/) + { + return false; + } + /*! @brief dump an integer @@ -16649,12 +16663,11 @@ class serializer // use a pointer to fill the buffer auto buffer_ptr = number_buffer.begin(); // NOLINT(llvm-qualified-auto,readability-qualified-auto,cppcoreguidelines-pro-type-vararg,hicpp-vararg) - const bool is_negative = std::is_same::value && !(x >= 0); // see issue #755 number_unsigned_t abs_value; unsigned int n_chars{}; - if (is_negative) + if (is_negative_number(x)) { *buffer_ptr = '-'; abs_value = remove_sign(static_cast(x));