cplusplus / CWG

Core Working Group
23 stars 7 forks source link

[class.cdtor] explicitly invoking a destructor from a constructor CWG2757 #435

Open sergey-anisimov-dev opened 9 months ago

sergey-anisimov-dev commented 9 months ago

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

Reference (section label): [class.cdtor]

Issue description: Basically, CWG2757 doesn't address the issue normatively, only informatively: it mentions using a delete-expr or reusing/releasing the storage for the object, while an explicit destructor call constitutes neither. The intention is there though, apparently, since the example supplemented features such a situation.

Suggested resolution: Insert , invoking a destructor for the object between evaluating a delete-expression with an operand that points to the object and or releasing or reusing (6.7.3 [basic.life]) the storage which the object occupies within the normative part of the resolution already proposed.

frederick-vs-ja commented 9 months ago

It might be simpler to add the prohibition to [class.cdtor] p4.

sergey-anisimov-dev commented 9 months ago

It might be simpler to add the prohibition to [class.cdtor] p4.

Might be a valid consideration. It's just that presently that clause is centered around the infamous static binding of the virtual functions in constructors/destructors. It might just be better to leave it be and group somewhat-related operations of "free"/"delete"/"destroy" together as CWG2757 originally intends. I personally would likely prefer seeing the prohibition of invoking the destructors explicitly stated rather than referred to in an obscure manner like Member functions, including virtual functions [class.virtual], except for the destructors, can be called during construction or destruction [class.base.init], as this is a double inversion: calling non-static member functions in out-of-life situations is generally disallowed by [basic.life#6.2]/[basic.life#7.2], then we state that it's ok only to disallow it specifically for the destructors just a couple of words after. On the other hand, this would be less contradictive: current resolution would disallow calling them specifically before explicitly reallowing a more generic action two clauses down... Perhaps it would indeed be better to state this in some other way after all.

jensmaurer commented 9 months ago

This seems to be pointing to a defect in the suggested wording for CWG2757. I've amended the suggested wording. (A delete-expression invokes the destructor.)

sergey-anisimov-dev commented 9 months ago

I'll just leave a note on the fact that this supposedly allows for delete-exprs that resolve into a destroying-delete (neither the destructor calls, nor the storage reuses/releases are guaranteed under such circumstances: the behavior is user-defined). This most likely isn't a problem though for the very same reasons.

languagelawyer commented 9 months ago

I think, given

During the construction or destruction of an object, invoking the destructor for the object … is undefined behavior.

http://eel.is/c++draft/class.dtor#18 can be completely removed, because, during construction/destruction, this is covered by the newly-proposed wording, and by http://eel.is/c++draft/basic.life#6.2 / http://eel.is/c++draft/basic.life#7.2 otherwise (plus http://eel.is/c++draft/basic.life#1.4 says the lifetime ends)

(OTOH, as it is known, http://eel.is/c++draft/class.dtor#18 conflicts with http://eel.is/c++draft/basic.life#9.sentence-1 a bit, in «non-trivial destructor» requirement)

sergey-anisimov-dev commented 9 months ago

That is the idea: perhaps you would be interested in reviewing this issue, @languagelawyer (especially concerning [basic.life#9]).