Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(532)

Unified Diff: base/bit_cast.h

Issue 2583353002: Make bit_cast fail if the source or dest are not trivially copyable (Closed)
Patch Set: bitcast: fixes Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/bit_cast_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/bit_cast.h
diff --git a/base/bit_cast.h b/base/bit_cast.h
index c9514bceef4d861f660aa200eaccc997c6588361..90dd925e86cd9f0510a255a2f3a945637a46e5ea 100644
--- a/base/bit_cast.h
+++ b/base/bit_cast.h
@@ -9,6 +9,7 @@
#include <type_traits>
#include "base/compiler_specific.h"
+#include "base/template_util.h"
#include "build/build_config.h"
// bit_cast<Dest,Source> is a template function that implements the equivalent
@@ -63,34 +64,10 @@ template <class Dest, class Source>
inline Dest bit_cast(const Source& source) {
static_assert(sizeof(Dest) == sizeof(Source),
"bit_cast requires source and destination to be the same size");
-
-#if (__GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1) || \
- (defined(__clang__) && defined(_LIBCPP_VERSION)))
- // GCC 5.1 contains the first libstdc++ with is_trivially_copyable.
- // Assume libc++ Just Works: is_trivially_copyable added on May 13th 2011.
- // However, with libc++ when GCC is the compiler the trait is buggy, see
- // crbug.com/607158, so fall back to the less strict variant for non-clang.
- static_assert(std::is_trivially_copyable<Dest>::value,
- "non-trivially-copyable bit_cast is undefined");
- static_assert(std::is_trivially_copyable<Source>::value,
- "non-trivially-copyable bit_cast is undefined");
-#elif HAS_FEATURE(is_trivially_copyable)
- // The compiler supports an equivalent intrinsic.
- static_assert(__is_trivially_copyable(Dest),
- "non-trivially-copyable bit_cast is undefined");
- static_assert(__is_trivially_copyable(Source),
- "non-trivially-copyable bit_cast is undefined");
-#elif COMPILER_GCC
- // Fallback to compiler intrinsic on GCC and clang (which pretends to be
- // GCC). This isn't quite the same as is_trivially_copyable but it'll do for
- // our purpose.
- static_assert(__has_trivial_copy(Dest),
- "non-trivially-copyable bit_cast is undefined");
- static_assert(__has_trivial_copy(Source),
- "non-trivially-copyable bit_cast is undefined");
-#else
- // Do nothing, let the bots handle it.
-#endif
+ static_assert(base::is_trivially_copyable<Dest>::value,
+ "bit_cast requires the destination type to be copyable");
+ static_assert(base::is_trivially_copyable<Source>::value,
+ "bit_cast requires the source type to be copyable");
Dest dest;
memcpy(&dest, &source, sizeof(dest));
« no previous file with comments | « no previous file | base/bit_cast_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698