mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
refactor: horrible_cast implementation
This commit is contained in:
parent
9325fbdf5c
commit
8c643308dd
@ -23,9 +23,8 @@ union temp {
|
||||
} // namespace detail_horrible_cast
|
||||
|
||||
template <typename T, typename U>
|
||||
constexpr auto horrible_cast(U &&in) noexcept
|
||||
-> std::enable_if_t<(sizeof(T) == sizeof(std::decay_t<U>)), T> {
|
||||
return detail_horrible_cast::temp<T, std::decay_t<U>>{std::forward<U>(in)}.out;
|
||||
constexpr T horrible_cast(U &&in) noexcept {
|
||||
return detail_horrible_cast::temp<T, U>{std::forward<U>(in)}.out;
|
||||
}
|
||||
|
||||
LIBIMP_NAMESPACE_END_
|
||||
|
||||
@ -143,7 +143,11 @@ TEST(utility, horrible_cast) {
|
||||
EXPECT_EQ(b.a_[0], 0);
|
||||
#endif
|
||||
|
||||
// imp::horrible_cast<std::uint32_t>(0ll);
|
||||
#if LIBIMP_ENDIAN_LIT
|
||||
EXPECT_EQ(imp::horrible_cast<std::uint32_t>(0xff00'0000'0001ll), 1);
|
||||
#else
|
||||
EXPECT_EQ(imp::horrible_cast<std::uint32_t>(0xff00'0000'0001ll), 0xff00);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(LIBIMP_CPP_17)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user