views:

1725

answers:

3

I downloaded Chromium's code base and ran across the WTF namespace.

namespace WTF {
    /*
     * C++'s idea of a reinterpret_cast lacks sufficient cojones.
     */
    template<typename TO, typename FROM>
    TO bitwise_cast(FROM in)
    {
        COMPILE_ASSERT(sizeof(TO) == sizeof(FROM), WTF_wtf_reinterpret_cast_sizeof_types_is_equal);
        union {
            FROM from;
            TO to;
        } u;
        u.from = in;
        return u.to;
    }
} // namespace WTF

Does this mean what I think it means? Could be so, the bitwise_cast implementation specified here will not compile if either TO or FROM is not a POD and is not (AFAIK) more powerful than C++ built in reinterpret_cast.

The only point of light I see here is the nobody seems to be using bitwise_cast in the Chromium project.

+3  A: 

Could be so, the bitwise_cast implementation specified here yields undefined behaviour if either TO or FROM is not a POD

If FROM or TO are not POD types, the compilation would fail with current C++ standard because you wouldn't be able to put them in union.

Artyom
Right you are, I'll correct the question.
Motti
Not sure. If your class contains a pointer-to-member, it's not a POD but it still can go in a union, I think.
MSalters
+20  A: 

Its short for Web Template Framework , provides commonly used functions all over the WebKit codebase.

cartman
A: 

It is to avoid the strict-aliasing optimization problem:

http://stackoverflow.com/questions/2906365/gcc-strict-aliasing-and-casting-through-a-union

Stan