Commit 2c93e885 authored by vegorov@chromium.org's avatar vegorov@chromium.org

Switch from template functions overloading to partial template specialization.

This should fix compilation on old GCC.

Review URL: http://codereview.chromium.org/6350012

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@6563 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 5ca89179
...@@ -760,20 +760,32 @@ static inline int TenToThe(int exponent) { ...@@ -760,20 +760,32 @@ static inline int TenToThe(int exponent) {
// you can use BitCast to cast one pointer type to another. This confuses gcc // 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 // enough that it can no longer see that you have cast one pointer type to
// another thus avoiding the warning. // 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> template <class Dest, class Source>
inline Dest BitCast(const Source& source) { struct BitCastHelper {
// Compile time assertion: sizeof(Dest) == sizeof(Source) STATIC_ASSERT(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; INLINE(static Dest cast(const Source& source)) {
memcpy(&dest, &source, sizeof(dest)); Dest dest;
return dest; memcpy(&dest, &source, sizeof(dest));
} return dest;
}
};
template <class Dest, class Source> template <class Dest, class Source>
inline Dest BitCast(Source* source) { struct BitCastHelper<Dest, Source*> {
return BitCast<Dest>(reinterpret_cast<uintptr_t>(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 } } // namespace v8::internal
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment