20 #ifndef RIPPLE_BASICS_SAFE_CAST_H_INCLUDED
21 #define RIPPLE_BASICS_SAFE_CAST_H_INCLUDED
31 template <
class Dest,
class Src>
33 (std::is_integral_v<Src> && std::is_integral_v<Dest>)&&(
36 ?
sizeof(Dest) >
sizeof(Src)
37 : sizeof(Dest) >= sizeof(Src));
39 template <
class Dest,
class Src>
40 inline constexpr std::
41 enable_if_t<std::is_integral_v<Dest> && std::is_integral_v<Src>, Dest>
45 std::is_signed_v<Dest> || std::is_unsigned_v<Src>,
46 "Cannot cast signed to unsigned");
47 constexpr
unsigned not_same =
48 std::is_signed_v<Dest> != std::is_signed_v<Src>;
50 sizeof(Dest) >=
sizeof(Src) + not_same,
51 "Destination is too small to hold all values of source");
52 return static_cast<Dest
>(s);
55 template <
class Dest,
class Src>
56 inline constexpr std::
57 enable_if_t<std::is_enum_v<Dest> && std::is_integral_v<Src>, Dest>
60 return static_cast<Dest
>(safe_cast<std::underlying_type_t<Dest>>(s));
63 template <
class Dest,
class Src>
64 inline constexpr std::
65 enable_if_t<std::is_integral_v<Dest> && std::is_enum_v<Src>, Dest>
75 template <
class Dest,
class Src>
76 inline constexpr std::
77 enable_if_t<std::is_integral_v<Dest> && std::is_integral_v<Src>, Dest>
81 !is_safetocasttovalue_v<Dest, Src>,
82 "Only unsafe if casting signed to unsigned or "
83 "destination is too small");
84 return static_cast<Dest
>(s);
87 template <
class Dest,
class Src>
88 inline constexpr std::
89 enable_if_t<std::is_enum_v<Dest> && std::is_integral_v<Src>, Dest>
92 return static_cast<Dest
>(unsafe_cast<std::underlying_type_t<Dest>>(s));
95 template <
class Dest,
class Src>
96 inline constexpr std::
97 enable_if_t<std::is_integral_v<Dest> && std::is_enum_v<Src>, Dest>