C++ Logo


Advanced search

Re: [std-proposals] Relax condition for potentially invoked destructor in constructor

From: Lénárd Szolnoki <cpp_at_[hidden]>
Date: Thu, 24 Feb 2022 17:48:20 +0000

While we are at it I think the proposal should relax the rules for
potentially invoked destructors in other contexts too. For example the
following also fails to compile currently, as the destructor of the
returned object is potentially invoked:

auto foo(std::unique_ptr<Incomplete>&& ptr) {
    return std::move(ptr);

In general, there could be local objects that are destroyed after the
returned object is created, and if any of those destructors throw, the
returned object needs to be destroyed as well. However in the
particular case above this doesn't apply, as there are no local objects.

So this could be relaxed so that the returned object's destructor is
potentially invoked only if at the point of return statement there are
local objects within their lifetime that have noexcept(false)
destructors. In practice this would be almost never.

Lénárd Szolnoki

Received on 2022-02-24 17:48:30