在QScopedPointer的实现中使用sizeof
The use of sizeof in the implementation of QScopedPointer
为了理解Qt如何防止不完整类型,我浏览了qscopedpointer.h
的头文件。相关部分如下:
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
时,编译会失败。但是数组和第二个sizeof
做什么?光有sizeof还不够吗?
使用一个数组,因此它的负大小将导致编译时错误。第二行通过确保使用IsIncompleteType
的实际大小来确保编译器不会跳过对sizeof(T)
的评估。
相关文章:
- 如果没有malloc,链表实现将失败
- 如何在c++中实现处理器调度模拟器
- 如何在c++中使用引用实现类似python的行为
- 实现无开销push_back的最佳方法是什么
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在BST的这个简单递归实现中消除警告
- 实现一个在集合上迭代的模板函数
- 我应该实现右值推送功能吗?我应该使用std::move吗
- 如何正确实现和访问运算符的各种自定义枚举器
- C++Union/Struct位域的实现和可移植性
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 在c++中实现LinkedList时,应出现未处理的错误
- 为左值和右值的包装器实现C++范围
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- 使用GSoap实现ONVIF
- 为什么 MSVC _count_of实现将 0 添加到 sizeof 的结果中?
- 在QScopedPointer的实现中使用sizeof
- 为什么定义了sizeof实现的结果
- sizeof不会显示实际大小,试图实现ip
- sizeof(int) >= 2 和 sizeof(long) >= 4 : 对于任何实现,这总是正确的吗?