Skip to content

Commit 331a979

Browse files
authored
refactor(generic): allow int64_t -> double conversion in to_double (#549)
1 parent 1bda2d0 commit 331a979

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

include/rfl/Generic.hpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,23 @@ class RFL_API Generic {
126126
}
127127

128128
/// Casts the underlying value to a double or returns an rfl::Error, if the
129-
/// underlying value is not a double.
129+
/// underlying value is not a number or the conversion would result in loss of
130+
/// precision.
130131
Result<double> to_double() const noexcept {
131132
return std::visit(
132133
[](auto _v) -> Result<double> {
133134
using V = std::remove_cvref_t<decltype(_v)>;
134135
if constexpr (std::is_same_v<V, double>) {
135136
return _v;
137+
} else if constexpr (std::is_same_v<V, int64_t>) {
138+
auto _d = static_cast<double>(_v);
139+
if (static_cast<int64_t>(_d) == _v) {
140+
return _d;
141+
} else {
142+
return error(
143+
"rfl::Generic: Could not cast the underlying value to a "
144+
"double without loss of precision.");
145+
}
136146
} else {
137147
return error(
138148
"rfl::Generic: Could not cast the underlying value to a "

0 commit comments

Comments
 (0)