From a43262ef884a4e4547bf347e83ab2b084ca9ef65 Mon Sep 17 00:00:00 2001 From: David Sobek Date: Mon, 24 Feb 2025 17:14:20 -0700 Subject: [PATCH] Fix clang test failures from Any.cast --- .../behaviortree_cpp/utils/convert_impl.hpp | 33 +++++-------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/include/behaviortree_cpp/utils/convert_impl.hpp b/include/behaviortree_cpp/utils/convert_impl.hpp index dffe17c98..a60f60c70 100644 --- a/include/behaviortree_cpp/utils/convert_impl.hpp +++ b/include/behaviortree_cpp/utils/convert_impl.hpp @@ -89,41 +89,24 @@ inline void checkLowerLimit(const From& from) template inline void checkTruncation(const From& from) { - // Handle integer to floating point - if constexpr(std::is_integral_v && std::is_floating_point_v) - { - // Check if value can be represented exactly in the target type - To as_float = static_cast(from); - From back_conv = static_cast(as_float); - if(back_conv != from) - { - throw std::runtime_error("Loss of precision in conversion to floating point"); - } - } // Handle floating point to integer if constexpr(std::is_floating_point_v && std::is_integral_v) { - if(from > static_cast(std::numeric_limits::max()) || - from < static_cast(std::numeric_limits::lowest()) || - from != std::nearbyint(from)) + if(from != std::nearbyint(from)) { throw std::runtime_error("Invalid floating point to integer conversion"); } } - // Handle other conversions - else + + To as_target = static_cast(from); + From back_to_source = static_cast(as_target); + if(from != back_to_source) { - if(from > static_cast(std::numeric_limits::max()) || - from < static_cast(std::numeric_limits::lowest())) - { - throw std::runtime_error("Value outside numeric limits"); - } - To as_target = static_cast(from); - From back_to_source = static_cast(as_target); - if(from != back_to_source) + if constexpr(std::is_integral_v && std::is_floating_point_v) { - throw std::runtime_error("Value truncated in conversion"); + throw std::runtime_error("Loss of precision in conversion to floating point"); } + throw std::runtime_error("Value truncated in conversion"); } }