From 331a9794b113315dd040621eb76e3f05ca982783 Mon Sep 17 00:00:00 2001 From: Noah <37805707+Curve@users.noreply.github.com> Date: Sun, 23 Nov 2025 17:36:14 +0100 Subject: [PATCH] refactor(generic): allow `int64_t` -> `double` conversion in `to_double` (#549) --- include/rfl/Generic.hpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/include/rfl/Generic.hpp b/include/rfl/Generic.hpp index eb4e90c7..32a23576 100644 --- a/include/rfl/Generic.hpp +++ b/include/rfl/Generic.hpp @@ -126,13 +126,23 @@ class RFL_API Generic { } /// Casts the underlying value to a double or returns an rfl::Error, if the - /// underlying value is not a double. + /// underlying value is not a number or the conversion would result in loss of + /// precision. Result to_double() const noexcept { return std::visit( [](auto _v) -> Result { using V = std::remove_cvref_t; if constexpr (std::is_same_v) { return _v; + } else if constexpr (std::is_same_v) { + auto _d = static_cast(_v); + if (static_cast(_d) == _v) { + return _d; + } else { + return error( + "rfl::Generic: Could not cast the underlying value to a " + "double without loss of precision."); + } } else { return error( "rfl::Generic: Could not cast the underlying value to a "