为了理解Qt如何防止类型不完整,我浏览了qscopedpointer.h.The相关部分的头文件,如下所示:
template <typename T>
struct QScopedPointerDeleter
{
static inline void cleanup(T *pointer)
{
// Enforce a complete type.
// If you get a compile error here, read the section on forward declared
// classes in the QScopedPointer documentation.
typedef char IsIncompleteType[ sizeof(T) ? 1 : -1 ];
(void) sizeof(IsIncompleteType);
delete pointer;
}
};我知道当在不完整的类型上使用sizeof时,编译将fail.But数组和第二个sizeof做什么?光是sizeof还不够吗?
发布于 2018-07-25 13:51:31
由于使用了数组,因此数组的大小为负数将导致编译时错误。第二行确保使用sizeof(T)的实际大小,从而确保编译器不会跳过IsIncompleteType的计算。
https://stackoverflow.com/questions/51511526
复制相似问题