webkit中对incomplete type指针的处理技巧

近日在研究webkit的时候发现了一个函数

template<typename T> inline void deleteOwnedPtr(T* ptr)
{
    typedef char known[sizeof(T) ? 1 : -1];
    if(sizeof(known))
        delete ptr;
}

一开始对这个函数非常费解,为什么作者不直接

delete ptr;

通过上stackoverflow提问然后查阅了一些资料后终于得到结果:这是用来防范错误释放incomplete type指针而导致的未知的行为。这样做之后,释放incomplete type指针将引发一个编译错误,使可能的错误能够及早发现。

原文是: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.

那么什么是incomplete type呢?顾名思义,就是一个只有声明没有定义完成的类型,比如前向声明。

举个例子

class SomeType;

void SomeFunction(SomeType* ptr)
{
    deleteOwnedPtr(ptr);
}

SomeType就是一个incomplete type,我们并不知道他的定义,如果我们企图释放该类型的指针,可能产生错误。

为了尽早避免出现这样的错误,我们先判断该指针指向的类型是否是incomplete type,然后人为引发一个编译期错误。

所以当ptr指向imcomplete type时,sizeof(T)结果为零,这时语句变为

typedef char[-1] known;
if (sizeof(known))
{
    ...
}

由于数组的大小不能为负,所以引发了编译错误,我们就能及早的发现可能出现的问题。

参考链接

http://stackoverflow.com/questions/10897998/i-cant-understand-the-deleteownedptr-function-in-webkit

原文地址:https://www.cnblogs.com/Jiajun/p/2772072.html