forked from OSchip/llvm-project
Revert "[ADT] restrict bit_cast to trivially-constructible To"
Of course some bots don't have std::is_trivially_constructible... llvm-svn: 342711
This commit is contained in:
parent
86f732a6a8
commit
b6eb21c29f
|
@ -20,12 +20,8 @@
|
||||||
|
|
||||||
namespace llvm {
|
namespace llvm {
|
||||||
|
|
||||||
// This implementation of bit_cast is different from the C++17 one in two ways:
|
|
||||||
// - It isn't constexpr because that requires compiler support.
|
|
||||||
// - It requires trivially-constructible To, to avoid UB in the implementation.
|
|
||||||
template <typename To, typename From
|
template <typename To, typename From
|
||||||
, typename = typename std::enable_if<sizeof(To) == sizeof(From)>::type
|
, typename = typename std::enable_if<sizeof(To) == sizeof(From)>::type
|
||||||
, typename = typename std::is_trivially_constructible<To>::type
|
|
||||||
#if (__has_feature(is_trivially_copyable) && defined(_LIBCPP_VERSION)) || \
|
#if (__has_feature(is_trivially_copyable) && defined(_LIBCPP_VERSION)) || \
|
||||||
(defined(__GNUC__) && __GNUC__ >= 5)
|
(defined(__GNUC__) && __GNUC__ >= 5)
|
||||||
, typename = typename std::enable_if<std::is_trivially_copyable<To>::value>::type
|
, typename = typename std::enable_if<std::is_trivially_copyable<To>::value>::type
|
||||||
|
@ -42,9 +38,17 @@ template <typename To, typename From
|
||||||
#endif
|
#endif
|
||||||
>
|
>
|
||||||
inline To bit_cast(const From &from) noexcept {
|
inline To bit_cast(const From &from) noexcept {
|
||||||
To to;
|
alignas(To) unsigned char storage[sizeof(To)];
|
||||||
std::memcpy(&to, &from, sizeof(To));
|
std::memcpy(&storage, &from, sizeof(To));
|
||||||
return to;
|
#if defined(__GNUC__)
|
||||||
|
// Before GCC 7.2, GCC thought that this violated strict aliasing.
|
||||||
|
#pragma GCC diagnostic push
|
||||||
|
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
|
||||||
|
#endif
|
||||||
|
return reinterpret_cast<To &>(storage);
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
#pragma GCC diagnostic pop
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace llvm
|
} // namespace llvm
|
||||||
|
|
Loading…
Reference in New Issue