Revision: 6563
Author: [email protected]
Date: Tue Feb  1 12:47:53 2011
Log: Switch from template functions overloading to partial template specialization.

This should fix compilation on old GCC.

Review URL: http://codereview.chromium.org/6350012
http://code.google.com/p/v8/source/detail?r=6563

Modified:
 /branches/bleeding_edge/src/utils.h

=======================================
--- /branches/bleeding_edge/src/utils.h Wed Dec 22 12:14:19 2010
+++ /branches/bleeding_edge/src/utils.h Tue Feb  1 12:47:53 2011
@@ -760,20 +760,32 @@
// you can use BitCast to cast one pointer type to another. This confuses gcc
 // enough that it can no longer see that you have cast one pointer type to
 // another thus avoiding the warning.
+
+// We need different implementations of BitCast for pointer and non-pointer
+// values. We use partial specialization of auxiliary struct to work around
+// issues with template functions overloading.
 template <class Dest, class Source>
-inline Dest BitCast(const Source& source) {
-  // Compile time assertion: sizeof(Dest) == sizeof(Source)
-  // A compile error here means your Dest and Source have different sizes.
- typedef char VerifySizesAreEqual[sizeof(Dest) == sizeof(Source) ? 1 : -1];
-
-  Dest dest;
-  memcpy(&dest, &source, sizeof(dest));
-  return dest;
-}
+struct BitCastHelper {
+  STATIC_ASSERT(sizeof(Dest) == sizeof(Source));
+
+  INLINE(static Dest cast(const Source& source)) {
+    Dest dest;
+    memcpy(&dest, &source, sizeof(dest));
+    return dest;
+  }
+};

 template <class Dest, class Source>
-inline Dest BitCast(Source* source) {
-  return BitCast<Dest>(reinterpret_cast<uintptr_t>(source));
+struct BitCastHelper<Dest, Source*> {
+  INLINE(static Dest cast(Source* source)) {
+    return BitCastHelper<Dest, uintptr_t>::
+        cast(reinterpret_cast<uintptr_t>(source));
+  }
+};
+
+template <class Dest, class Source>
+inline Dest BitCast(const Source& source) {
+  return BitCastHelper<Dest, Source>::cast(source);
 }

 } }  // namespace v8::internal

--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to