Is there a "safe" alternative to static_cast
in C++11/14 or a library which implements this functionality?
By "safe" I mean the cast should only allow casts which do not lose precision. So a cast from int64_t
to int32_t
would only be allowed if the number fits into a int32_t
and else an error is reported.
There's gsl::narrow
narrow //
narrow<T>(x)
isstatic_cast<T>(x)
ifstatic_cast<T>(x) == x
or it throwsnarrowing_error
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With