From 25ed41033172dca5f299687b4ed30d5f275d58f8 Mon Sep 17 00:00:00 2001 From: Isaac Nickaein Date: Wed, 19 May 2021 14:21:25 +0430 Subject: [PATCH] use templated is_negative --- include/nlohmann/detail/output/serializer.hpp | 17 +++++++++++++++-- single_include/nlohmann/json.hpp | 17 +++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index 865b8904e2..d8f412f65a 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -659,6 +659,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 @@ -701,12 +715,11 @@ class serializer // use a pointer to fill the buffer auto buffer_ptr = number_buffer.begin(); - 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 a70aaf8cbc..62195a1370 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -16096,6 +16096,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 @@ -16138,12 +16152,11 @@ class serializer // use a pointer to fill the buffer auto buffer_ptr = number_buffer.begin(); - 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));