[PATCH] D52332: [ADT] restrict bit_cast to trivially-constructible To
JF Bastien via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 20 22:22:42 PDT 2018
This revision was automatically updated to reflect the committed changes.
Closed by commit rL342710: [ADT] restrict bit_cast to trivially-constructible To (authored by jfb, committed by ).
Repository:
rL LLVM
https://reviews.llvm.org/D52332
Files:
llvm/trunk/include/llvm/ADT/bit.h
Index: llvm/trunk/include/llvm/ADT/bit.h
===================================================================
--- llvm/trunk/include/llvm/ADT/bit.h
+++ llvm/trunk/include/llvm/ADT/bit.h
@@ -20,8 +20,12 @@
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
, 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)) || \
(defined(__GNUC__) && __GNUC__ >= 5)
, typename = typename std::enable_if<std::is_trivially_copyable<To>::value>::type
@@ -38,17 +42,9 @@
#endif
>
inline To bit_cast(const From &from) noexcept {
- alignas(To) unsigned char storage[sizeof(To)];
- std::memcpy(&storage, &from, sizeof(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
+ To to;
+ std::memcpy(&to, &from, sizeof(To));
+ return to;
}
} // namespace llvm
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52332.166405.patch
Type: text/x-patch
Size: 1410 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180921/f779b549/attachment.bin>
More information about the llvm-commits
mailing list