Open frederick-vs-ja opened 6 months ago
This is undefined behavior because the explicit destructor call on b
modifies a const object, per [dcl.type.cv] p4.
If your counterargument is that b
is outside its lifetime, I think we should rephrase "outside its lifetime" to also including "during its period of destruction".
If your counterargument is that
b
is outside its lifetime, I think we should rephrase "outside its lifetime" to also including "during its period of destruction".
Hmm... it seems intended that const
and volatile
shouldn't be in effect during destruction per [class.dtor] p5:
[...] A destructor can be invoked for a
const
,volatile
orconst volatile
object.const
andvolatile
semantics ([dcl.type.cv]) are not applied on an object under destruction. They stop being in effect when the destructor for the most derived object ([intro.object]) starts.
Perhaps there should be some additional rules for objects that are variables.
Full name of submitter (unless configured in github; will be published with the issue): Jiang An
Reference (section label): [expr.const], [class.dtor]
Link to reflector thread (if any):
Issue description:
The execution of the following program doesn't seem to have undefined behavior according to the current standard wording, because terminating via
_Exit
doesn't execute destructor forb
([support.start.term] p3) and henceb
won't be destroyed twice.However, segmentation fault can be raised on some implementations (especially when optimizatoin is disabled, Godbolt link), which shouldn't happen if there's no UB.
The current wording seemingly forbids implementations to place
b
into read-only storage, even if it is usable in constant expressions and has nomutable
subobject.Suggested resolution:
Option A: NAD, which means that the existing strategy is non-conforming.
Option B: Disallowing explicit destructor invocation for objects usable constant expressions.
Change [expr.call] p4 on the top of the current possible resolution of CWG2839:
Option C: Allowing explicit destructor invocation for objects usable constant expressions with modification disallowed.
Change [class.dtor] p16 as indicated: