我不明白 boost::checked_delete 的目的。文档说:
The C++ Standard allows, in 5.3.5/5, pointers to incomplete class types to be deleted with a delete-expression. When the class has a non-trivial destructor, or a class-specific operator delete, the behavior is undefined. Some compilers issue a warning when an incomplete type is deleted, but unfortunately, not all do, and programmers sometimes ignore or disable warnings.
The supplied function and class templates can be used to prevent these problems, as they require a complete type, and cause a compilation error otherwise.
因此,C++ 标准允许您删除不完整的类型,如果该类型具有非平凡的析构函数,则会导致未定义的行为。什么?一个不完整的类型怎么可能有任何析构函数呢?不完整的类型不就是原型(prototype)吗?
最佳答案
不完整类型的最常见示例是仅被声明的类型:
// this file does not include the definition of foo
class foo;
void bad(foo *f)
{
delete f; // undefined behavior if there exists foo::~foo
}
实际上,foo 的定义可能是这样的:
class foo
{
public:
~foo() { ... };
};
但如果顶部代码没有“看到”类定义而只是看到类声明,则代码将编译。
关于c++ - boost::checked_delete 的目的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2852952/