调用琐碎析构函数有什么作用?

问题描述 投票:2回答:1

对琐碎析构函数的调用是否会结束对象的生命周期?我读了thisthis,但找不到很好的解释。这些线程声明琐碎的析构函数调用无效,并且类似struct A { int x; } a; a.~A(); a.~A();的代码合法。但我在标准中找到了该示例:

struct C { };
void f() {
    C * pc = new C;
    using C2 = C;
    pc->C::~C2(); // OK, destroys *pc
    C().C::~C(); // undefined behavior: temporary of type C destroyed twice
    using T = int;
    0 .T::~T(); // OK, no effect
    0.T::~T(); // error: 0.T is a user-defined-floating-point-literal (5.13.8)
}

C在这里具有微不足道的析构函数,但对C类型的对象进行两次销毁仍然具有未定义的行为吗?

c++ destructor lifetime
1个回答
0
投票

从C ++ 20琐碎的析构函数调用开始,结束了对象的生命周期。在此之前,它们没有,并且多次调用析构函数是有效的。

这在C ++ 20草案(N4861)的(参考)[diff.cpp17.basic]/1节中作为重大更改进行了详细说明。

© www.soinside.com 2019 - 2024. All rights reserved.