98

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.

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Motti
  • 110,860
  • 49
  • 189
  • 262
  • 4
    Probably a good idea to quote the "NO WARRANTIES" part. – MSalters May 07 '09 at 13:21
  • @KennyTM please see http://meta.stackexchange.com/questions/45844/is-the-tag-wtf-acceptable for a discussion of how to tag this question – Earlz May 31 '10 at 06:15
  • Wow, that class is not described by "Web Template Framework." It's just a convenient abbreviation. – macetw Jun 06 '14 at 12:13

3 Answers3

91

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

Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248
ismail
  • 46,010
  • 9
  • 86
  • 95
  • 2
    as in C++ templates doing common stuff :) – ismail Jan 31 '12 at 12:17
  • any way of cloning that library? – Victor Jan 05 '17 at 11:22
  • Citations please. Does it do templating like Mustache or JSP? – adib Mar 23 '17 at 08:53
  • @adib No, the "template" really refers to C++ templates and nothing else. See https://trac.webkit.org/wiki/SourceDirectory and https://webkit.org/blog/6161/locking-in-webkit/ (for WebKit sources) and https://chromium.googlesource.com/chromium/src/+/master/third_party/blink/renderer/platform/wtf/README.md (for Blink source). – Timothy Gu Apr 27 '20 at 20:46
5

It is to avoid the strict-aliasing optimization problem:

gcc, strict-aliasing, and casting through a union

Community
  • 1
  • 1
Stan
  • 51
  • 1
  • 1
2

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
  • 31,019
  • 21
  • 127
  • 215
  • 1
    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 May 07 '09 at 13:22