llvm.org GIT mirror llvm / 0c02f21
[ADT] bit_cast: check for is_trivially_copyable more portably Summary: It turns out that isPodLike isn't a good workaround for is_trivially_copyable for bit_cast's purpose. In D51872 Louis points out that tuple and pair really aren't a good fit, and for bit_cast I want to capture array. This patch instead checks is_trivially_copyable directly in bit_cast for all but GCC 4.x. In GCC 4.x developers only check for sizeof match, which means any mistake they make will succeed locally and fail on the bots. Realistically that's few developers and they'll be left behind once we upgrade past C++11. This will allow using bit_cast with std::array. Subscribers: dexonsmith, llvm-commits, ldionne, rsmith Differential Revision: https://reviews.llvm.org/D51888 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@341865 91177308-0d34-0410-b5e6-96231b3b80d8 JF Bastien 1 year, 10 months ago
1 changed file(s) with 19 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
1313 #ifndef LLVM_ADT_BIT_H
1414 #define LLVM_ADT_BIT_H
1515
16 #include "llvm/Support/type_traits.h"
16 #include "llvm/Support/Compiler.h"
1717 #include
18 #include
1819
1920 namespace llvm {
2021
21 template
22 typename = typename std::enable_if::type,
23 typename = typename std::enable_if::value>::type,
24 typename = typename std::enable_if::value>::type>
22 template
23 , typename = typename std::enable_if::type
24 #if (__has_feature(is_trivially_copyable) && defined(_LIBCPP_VERSION)) || \
25 (defined(__GNUC__) && __GNUC__ >= 5)
26 , typename = typename std::enable_if::value>::type
27 , typename = typename std::enable_if::value>::type
28 #elif __has_feature(is_trivially_copyable)
29 , typename = typename std::enable_if<__is_trivially_copyable::value>::type
30 , typename = typename std::enable_if<__is_trivially_copyable::value>::type
31 #else
32 // This case is GCC 4.x. clang with libc++ or libstdc++ never get here. Unlike
33 // llvm/Support/type_traits.h's isPodLike we don't want to provide a
34 // good-enough answer here: developers in that configuration will hit
35 // compilation failures on the bots instead of locally. That's acceptable
36 // because it's very few developers, and only until we move past C++11.
37 #endif
38 >
2539 inline To bit_cast(const From &from) noexcept {
2640 alignas(To) unsigned char storage[sizeof(To)];
2741 std::memcpy(&storage, &from, sizeof(To));