cplusplus / CWG

Core Working Group
23 stars 7 forks source link

CWG2778 [expr.const] Conflict between constant destruction and a mutable member of a class type #377

Open frederick-vs-ja opened 1 year ago

frederick-vs-ja commented 1 year ago

Full name of submitter (unless configured in github; will be published with the issue): Jiang An

Reference (section label): [expr.const]

Link to reflector thread (if any):

Issue description:

[expr.const] p9.2 states that the lifetime of mutable subobjects is not considered to start within the hypothetical expression when determining whether an object has constant destruction. But [expr.const] p5.17 seemingly states that a destruction call of such a member would make the hypothetical expression not a core constant expression, which may in turn make the complete object not have constant destruction.

Given the following example:

struct Foo {};
struct Bar {
  mutable Foo f;
};
constexpr Bar b{};

when determining whether b has constant destruction, the hypothetical expression E need to call the destructor for b.f, and the lifetime of b.f is not considered to start within E. As a result, [expr.const] p9 and [expr.const] p5.17 would make b not have constant destruction, and thus make the program ill-formed.

The current wording is introducing incompatibility with C++17 (where trivial destructibility was used). Implementations accept this example in their C++20 modes (Godbolt link).

Perhaps we should exclude [expr.const] p5.17 when determining whether the hypothetical expression is a core constant expression.

Suggested resolution:

jensmaurer commented 1 year ago

CWG2778