Skip to content

Commit 441eeba

Browse files
eisenwavetkoeppe
authored andcommitted
CWG3100 Destruction order for objects with static storage duration
Fixes NB CA 045 (C++26 CD).
1 parent dc9b135 commit 441eeba

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

source/basic.tex

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7542,21 +7542,45 @@
75427542
apply only to complete objects not having constant destruction.
75437543

75447544
\pnum
7545-
If the completion of the constructor or dynamic initialization of an object with static
7546-
storage duration strongly happens before that of another, the completion of the destructor
7547-
of the second is sequenced before the initiation of the destructor of the first.
7548-
If the completion of the constructor or dynamic initialization of an object with thread
7549-
storage duration is sequenced before that of another, the completion of the destructor
7550-
of the second is sequenced before the initiation of the destructor of the first.
7545+
If the deemed construction\iref{dcl.init.general} of a complete object with static
7546+
storage duration strongly happens before that of another, the completion of the destruction
7547+
of the second is sequenced before the initiation of the destruction of the first.
7548+
If the deemed construction of a complete object with thread
7549+
storage duration is sequenced before that of another, the completion of the destruction
7550+
of the second is sequenced before the initiation of the destruction of the first.
75517551
If an object is
75527552
initialized statically, the object is destroyed in the same order as if
7553-
the object was dynamically initialized. For an object of array or class
7554-
type, all subobjects of that object are destroyed before any block
7555-
variable with static storage duration initialized during the construction
7556-
of the subobjects is destroyed.
7553+
the object was dynamically initialized.
75577554
If the destruction of an object with static or thread storage duration
75587555
exits via an exception,
75597556
the function \tcode{std::terminate} is called\iref{except.terminate}.
7557+
\begin{example}
7558+
In the following program,
7559+
the elements of \tcode{a} are destroyed,
7560+
followed by \tcode{dt}, and
7561+
finally by the two \tcode{Btemp} objects:
7562+
\begin{codeblock}
7563+
struct DTemp { ~DTemp(); };
7564+
struct Temp {
7565+
~Temp() {
7566+
static DTemp dt;
7567+
}
7568+
};
7569+
struct BTemp {
7570+
~BTemp();
7571+
};
7572+
struct A {
7573+
const BTemp &tb;
7574+
~A();
7575+
};
7576+
A a[] = { (Temp(), BTemp()), BTemp() };
7577+
7578+
int main() {}
7579+
\end{codeblock}
7580+
If the array \tcode{a} were an object with automatic storage duration,
7581+
the \tcode{Btemp} temporaries would be destroyed
7582+
as each element of the array is destroyed\iref{class.temporary}.
7583+
\end{example}
75607584

75617585
\pnum
75627586
If a function contains a block variable of static or thread storage duration that has been
@@ -7572,12 +7596,13 @@
75727596
\pnum
75737597
\indextext{\idxcode{atexit}}%
75747598
\indexlibraryglobal{atexit}%
7575-
If the completion of the initialization of an object with static storage
7599+
If the deemed construction of a complete object with static storage
75767600
duration strongly happens before a call to \tcode{std::atexit}~(see
75777601
\libheader{cstdlib}, \ref{support.start.term}), the call to the function passed to
7578-
\tcode{std::atexit} is sequenced before the call to the destructor for the object. If a
7579-
call to \tcode{std::atexit} strongly happens before the completion of the initialization of
7580-
an object with static storage duration, the call to the destructor for the
7602+
\tcode{std::atexit} is sequenced before the initiation of the destruction of the object.
7603+
If a call to \tcode{std::atexit} strongly happens before the deemed construction of
7604+
a complete object with static storage duration,
7605+
the completion of the destruction of the
75817606
object is sequenced before the call to the function passed to \tcode{std::atexit}. If a
75827607
call to \tcode{std::atexit} strongly happens before another call to \tcode{std::atexit}, the
75837608
call to the function passed to the second \tcode{std::atexit} call is sequenced before

source/declarations.tex

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5437,8 +5437,13 @@
54375437
\grammarterm{designated-initializer-list}.
54385438

54395439
\pnum
5440-
An object whose initialization has completed
5441-
is deemed to be constructed,
5440+
The \defnadj{deemed}{construction} of an object occurs when
5441+
its initialization completes;
5442+
for the purposes of~\ref{basic.start.term},
5443+
if the initialization is a full-expression,
5444+
deemed construction occurs when
5445+
the evaluation of that full-expression completes.
5446+
The object is deemed to be constructed,
54425447
even if the object is of non-class type or
54435448
no constructor of the object's class
54445449
is invoked for the initialization.

0 commit comments

Comments
 (0)