卧槽卧槽在 WebKit 代码库中代表什么?

我下载了 的代码库,然后运行了 卧槽命名空间

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

这是我想的那个意思吗?可能是这样的,如果 TOFROM不是 POD,并且(AFAIK)不比构建在 reinterpret_cast中的 C + + 更强大,那么这里指定的 bitwise_cast实现将不会编译。

我在这里看到的唯一亮点是似乎没有人在 Chromium 项目中使用 bitwise_cast

21348 次浏览

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.

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

It is to avoid the strict-aliasing optimization problem:

gcc, strict-aliasing, and casting through a union