c++ - boost::checked_delete 的目的

标签 c++ boost standards

我不明白 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/

相关文章:

C++ 构造函数和静态成员

c++ - 在 linux ubuntu 服务器 11.10 (vps) 上使用 asio 使用 C++ 进行编译

c++ - 为什么以下模板声明格式错误?

c++ - Opencv Flannbasedmatcher

c++ - C++:在constexpr构造函数中初始化成员数组

c++ - BOOST_FOREACH 可以定制为指针类型吗?

algorithm - 为什么是 `copy_n` 、 `fill_n` 和 `generate_n` ?

c++ - 带有额外可选模板参数的标准库容器?

c++ - 从文本文件创建 2d int 表。输出(可能)是内存块

c++ - BGL : bundled object without a default constructor?