实际销毁对象是否需要销毁运算符的销毁形式? [英] Is destroying form of operator delete required to actually destroy the object?

查看:67
本文介绍了实际销毁对象是否需要销毁运算符的销毁形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 20添加了以 std :: destroying_delete_t 参数区分的 operator delete 破坏形式.它会导致 delete 表达式在调用 operator delete 之前不再破坏对象.

C++20 has added destroying form of operator delete distinguished by the std::destroying_delete_t parameter. It causes delete expression to no longer destroy the object prior to invoking operator delete.

目的是允许在显式调用对象的析构函数并释放内存之前,以依赖于对象状态的方式自定义删除.

The intention is to allow customization of deletion in a way that depends on the object's state, before explicitly invoking the object's destructor and deallocating memory.

但是,我不清楚我在实现这种运算符时是否真的需要销毁该对象.具体来说,是否可以让我拥有一个静态对象池,并将其分发给可以将其视为动态分配对象的用户?这样,在对象上执行的 delete 表达式只会将其返回到池中,而不会破坏它.例如,以下程序是否定义明确?

However, it isn't clear to me if, when implementing such an operator, I'm actually required to destroy the object. Specifically, am I allowed to have a pool of static objects, and give them out to users who can subsequently treat them as-if they were dynamically allocated? Such that delete expression executed on the object will merely return it to the pool without destroying it. For example, is the following program well-defined?

#include <new>
 
struct A {
    virtual ~A() = default;
};

// 'Regular' dynamically allocated objects
struct B : A {
    static A* create() {
        return new B();
    }

private:
    B() = default;
};

// Pooled, statically allocated objects
struct C : A {
    static A* create() {
        for (auto& c: pool) {
            if (!c.in_use) {
                c.in_use = true;
                return &c;
            }
        }
        throw std::bad_alloc();
    }

private:
    static C pool[3];

    bool in_use = false;

    C() = default;

    void operator delete(C *c, std::destroying_delete_t) {
        c->in_use = false;
    }
};

C C::pool[3];

// Delete them identically via the common interface.
void do_something_and_delete(A* a) {
    delete a;
}

int main() {
    do_something_and_delete(B::create());
    do_something_and_delete(B::create());
    do_something_and_delete(C::create());
    do_something_and_delete(C::create());
}

推荐答案

销毁删除运算符的目的,已定义

The purpose of destroying delete operators, as defined by its proposal, is to effectively deal with the ability to create and destroy objects whose deallocation and destruction needs access to the object, for one reason or another. It does this by preventing the automatic invocation of the object's destructor when you invoke delete on objects with a destroying operator delete function. The (still live) object is then passed to the destroying operator delete, so that it can do the deallocation and destruction business.

其目的不是 使该语句删除任何内容; 对用户撒谎有关该语句的作用.但是,由于功能的一种使用情况(没有 virtual 功能的虚拟析构函数)的结果,该功能可能(被用来)欺骗用户.

Its purpose is not to make the statement delete whatever; lie to the user about what this statement accomplishes. But as a consequence of one of the use cases of the feature (virtual destructors without virtual functions), the feature can be (ab)used to lie to the user.

在输入对象的析构函数时(或在重新使用/释放存储时)对象的生存期结束.如果(销毁)销毁运算符delete来防止调用该析构函数,则 delete 删除对象将不会终止其生存期.

The lifetime of an object ends when its destructor is entered (or when the storage is reused/released). If a destroying operator delete is (ab)used to prevent calling that destructor, then deleteing the object will not end its lifetime.

但是对用户撒谎是一个坏主意,您不应该这样做.

But lying to the user is a bad idea and you shouldn't do it.

这篇关于实际销毁对象是否需要销毁运算符的销毁形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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