Question

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.

Was it helpful?

Solution

It’s short for Web Template Framework and provides commonly used functions all over the WebKit codebase.

OTHER TIPS

It is to avoid the strict-aliasing optimization problem:

gcc, strict-aliasing, and casting through a union

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top