From 26cddc6cde5f496b80d5bff98a3bdfabd70fb1ba Mon Sep 17 00:00:00 2001 From: Tianyi Chen Date: Wed, 27 Nov 2024 15:04:33 -0800 Subject: [PATCH] Not casting float to int, it violates strict aliasing rule --- include/nlohmann/detail/input/binary_reader.hpp | 12 ++++++------ single_include/nlohmann/json.hpp | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 17a760f5d1..395feccf8c 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -2803,17 +2803,17 @@ class binary_reader { return; } - // convert float types to int types of the same size - using swap_t = std::conditional::type>::type; - swap_t& number_ref = reinterpret_cast(number); - number_ref = std::byteswap(number_ref); -#else + if constexpr(std::is_integral_v) + { + number = std::byteswap(number); + return; + } +#endif auto* ptr = reinterpret_cast(&number); for (std::size_t i = 0; i < sz / 2; ++i) { std::swap(ptr[i], ptr[sz - i - 1]); } -#endif } /* diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 2d3de1c770..07b2c22c20 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -12101,17 +12101,17 @@ class binary_reader { return; } - // convert float types to int types of the same size - using swap_t = std::conditional::type>::type; - swap_t& number_ref = reinterpret_cast(number); - number_ref = std::byteswap(number_ref); -#else + if constexpr(std::is_integral_v) + { + number = std::byteswap(number); + return; + } +#endif auto* ptr = reinterpret_cast(&number); for (std::size_t i = 0; i < sz / 2; ++i) { std::swap(ptr[i], ptr[sz - i - 1]); } -#endif } /*