| 241 | |
| 242 | template <typename SRC, typename TO> |
| 243 | inline bool ValidCast(const SRC& val) |
| 244 | { |
| 245 | // First check numeric limits |
| 246 | if constexpr(std::is_arithmetic_v<SRC> && std::is_arithmetic_v<TO>) |
| 247 | { |
| 248 | // Handle conversion to floating point |
| 249 | if constexpr(std::is_floating_point_v<TO>) |
| 250 | { |
| 251 | if constexpr(std::is_integral_v<SRC>) |
| 252 | { |
| 253 | // For integral to float, check if we can represent the value exactly |
| 254 | TO as_float = static_cast<TO>(val); |
| 255 | SRC back_conv = static_cast<SRC>(as_float); |
| 256 | return back_conv == val; |
| 257 | } |
| 258 | } |
| 259 | // Handle conversion to integral |
| 260 | else if constexpr(std::is_integral_v<TO>) |
| 261 | { |
| 262 | if(val > static_cast<SRC>(std::numeric_limits<TO>::max()) || |
| 263 | val < static_cast<SRC>(std::numeric_limits<TO>::lowest())) |
| 264 | { |
| 265 | return false; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | TO as_target = static_cast<TO>(val); |
| 271 | SRC back_to_source = static_cast<SRC>(as_target); |
| 272 | return val == back_to_source; |
| 273 | } |
| 274 | |
| 275 | template <typename T> |
| 276 | inline bool isCastingSafe(const std::type_index& type, const T& val) |
nothing calls this directly
no outgoing calls
no test coverage detected