【发布时间】:2017-01-07 12:42:46
【问题描述】:
从v8-0.2.5获取的代码
/**
* Checks whether two handles are the same.
* Returns true if both are empty, or if the objects
* to which they refer are identical.
* The handles' references are not checked.
*/
template <class S> bool operator==(Handle<S> that) {
void** a = reinterpret_cast<void**>(**this);
void** b = reinterpret_cast<void**>(*that);
if (a == 0) return b == 0;
if (b == 0) return false;
return *a == *b;
}
Handle 重载运算符* 以便 **this 和 *that 返回类型 T*。
好像是这样
void* a = reinterpret_cast<void*>(**this);
void* b = reinterpret_cast<void*>(*that);
return a == b;
也会很好用吗?
【问题讨论】:
-
这种魔法过去是在旧的 C 代码中完成的,通过 void* 类型的引用变量传递。我认为,如果您将 return 更改为 a == b 并具有 b 以及 void* 类型的 be ,它将运行良好。
-
如果取消引用
Handle<T>会产生T*,那么这段代码就有很大的缺陷,因为比较会产生误报和漏报:如果sizeof(T) < sizeof(void*)你会得到误报,因为这样做的东西不属于的对象包含在比较中,如果sizeof(T) > sizeof(void*)会得到误报,因为只比较对象的前四个/八个字节。
标签: c++ v8 reinterpret-cast