20#ifndef RIPPLE_BASICS_SAFE_CAST_H_INCLUDED
21#define RIPPLE_BASICS_SAFE_CAST_H_INCLUDED
31template <
class Src,
class Dest>
35 ?
sizeof(Dest) >
sizeof(Src)
36 : sizeof(Dest) >= sizeof(Src));
38template <
class Dest,
class Src>
45 "Cannot cast signed to unsigned");
46 constexpr unsigned not_same =
49 sizeof(Dest) >=
sizeof(Src) + not_same,
50 "Destination is too small to hold all values of source");
51 return static_cast<Dest
>(s);
54template <
class Dest,
class Src>
59 return static_cast<Dest
>(safe_cast<std::underlying_type_t<Dest>>(s));
62template <
class Dest,
class Src>
74template <
class Dest,
class Src>
81 "Only unsafe if casting signed to unsigned or "
82 "destination is too small");
83 return static_cast<Dest
>(s);
86template <
class Dest,
class Src>
91 return static_cast<Dest
>(unsafe_cast<std::underlying_type_t<Dest>>(s));
94template <
class Dest,
class Src>
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > safe_cast(Src s) noexcept
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > unsafe_cast(Src s) noexcept