[英]I can't understand the deleteOwnedPtr function in Webkit
In found this function in Webkit 在Webkit中找到此功能
template<typename T> inline void deleteOwnedPtr(T* ptr)
{
typedef char known[sizeof(T) ? 1 : -1];
if(sizeof(known))
delete ptr;
}
Why they don't use 他们为什么不使用
if (sizeof(T))
delete ptr;
What does it mean 这是什么意思
char known[-1];
found the explanation in the webkit-dev mailing list. 在webkit-dev邮件列表中找到了说明。 It raises compiler errors if someone is trying to delete incomplete types.
如果有人试图删除不完整的类型,则会引起编译器错误。
https://lists.webkit.org/pipermail/webkit-dev/2010-November/015051.html https://lists.webkit.org/pipermail/webkit-dev/2010-November/015051.html
If we delete a pointer and the object has incomplete type, we get undefined behavior.
如果删除指针,并且对象的类型不完整,则会得到未定义的行为。 Instead this code causes compilation to fail if the object has incomplete type.
相反,如果对象的类型不完整,此代码将导致编译失败。 The use of a negative number for the size of an array is a way to guarantee we get a compilation error.
对数组的大小使用负数是一种保证我们得到编译错误的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.