升压目的:: checked_delete [英] Purpose of boost::checked_delete

查看:114
本文介绍了升压目的:: checked_delete的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白的boost :: checked_delete的目的。文档说:

I don't understand the purpose of boost::checked_delete. The documentation says:

C ++标准允许的话,在5.3.5 / 5,
  指针不完整的类类型
  与删除-EX pression删除。
  当类具有一个非平凡
  析构函数或类特定
  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.

提供的函数和类
  模板可以用prevent这些
  的问题,因为它们需要一个完整的
  键入,并导致编译错误
  否则。

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 ++标准允许你删除不完全类型,这将导致未定义的行为,如果该类型有一个不平凡的析构函数。什么?一个不完全类型怎么能有任何析构函数呢?是不是不完全类型只是一个原型?

So the C++ standard allows you to delete incomplete types, which causes undefined behavior if the type has a non-trivial destructor. What? How can an incomplete type have any destructor at all? Isn't an incomplete type just a prototype?

推荐答案

不完全类型的最常见的例子是,只被宣告之一:

The most common example of an incomplete type is one that has only been declared:

// 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的定义可能是这样的:

In reality, the definition of foo may look like this:

class foo
{
public:
    ~foo() { ... };
};

但是,如果顶部code还没有看到类定义,只是看到了类的声明中,code编译。

But if the top code has not 'seen' the class definition and just sees the class declaration, the code will compile.

这篇关于升压目的:: checked_delete的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆