Skip to content

Commit 84a5ebb

Browse files
authored
Merge 2019-07 CWG Motion 17
P1143R2 Adding the constinit keyword Fixes #2997.
2 parents c458614 + fb1528d commit 84a5ebb

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

source/compatibility.tex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,9 @@
18191819
The \tcode{consteval} keyword is added to
18201820
declare immediate functions\iref{dcl.constexpr}.
18211821
\item
1822+
The \keyword{constinit} keyword is added to
1823+
prevent unintended dynamic initialization\iref{dcl.constinit}.
1824+
\item
18221825
The \tcode{co_await}, \tcode{co_yield}, and \tcode{co_return} keywords are added
18231826
to enable the definition of coroutines \iref{dcl.fct.def.coroutine}.
18241827
\item

source/declarations.tex

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@
257257
\keyword{typedef}\br
258258
\keyword{constexpr}\br
259259
\keyword{consteval}\br
260+
\keyword{constinit}\br
260261
\keyword{inline}
261262
\end{bnf}
262263

@@ -276,8 +277,9 @@
276277
Each \grammarterm{decl-specifier}
277278
shall appear at most once in a complete \grammarterm{decl-specifier-seq},
278279
except that \tcode{long} may appear twice.
279-
The \tcode{constexpr} and \tcode{consteval} \grammarterm{decl-specifier}{s}
280-
shall not both appear in a \grammarterm{decl-specifier-seq}.
280+
At most one of
281+
the \keyword{constexpr}, \keyword{consteval}, and \keyword{constinit} keywords
282+
shall appear in a \grammarterm{decl-specifier-seq}.
281283

282284
\pnum
283285
\indextext{ambiguity!declaration type}%
@@ -1015,6 +1017,35 @@
10151017
\end{codeblock}
10161018
\end{example}
10171019

1020+
\rSec2[dcl.constinit]{The \tcode{constinit} specifier}
1021+
\indextext{specifier!\idxcode{constinit}}
1022+
1023+
\pnum
1024+
The \keyword{constinit} specifier shall be applied only
1025+
to a declaration of a variable with static or thread storage duration.
1026+
If the specifier is applied to any declaration of a variable,
1027+
it shall be applied to the initializing declaration.
1028+
No diagnostic is required if no \keyword{constinit} declaration
1029+
is reachable at the point of the initializing declaration.
1030+
1031+
\pnum
1032+
If a variable declared with the \keyword{constinit} specifier has
1033+
dynamic initialization\iref{basic.start.dynamic}, the program is ill-formed.
1034+
\begin{note}
1035+
The \keyword{constinit} specifier ensures that the variable
1036+
is initialized during static initialization\iref{basic.start.static}.
1037+
\end{note}
1038+
1039+
\pnum
1040+
\begin{example}
1041+
\begin{codeblock}
1042+
const char * g() { return "dynamic initialization"; }
1043+
constexpr const char * f(bool p) { return p ? "constant initializer" : g(); }
1044+
constinit const char * c = f(true); // OK
1045+
constinit const char * d = f(false); // ill-formed
1046+
\end{codeblock}
1047+
\end{example}
1048+
10181049
\rSec2[dcl.inline]{The \tcode{inline} specifier}%
10191050
\indextext{specifier!\idxcode{inline}}
10201051

source/lex.tex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -732,8 +732,9 @@
732732
\keyword{concept} \\
733733
\keyword{const} \\
734734
\keyword{consteval} \\
735-
\columnbreak
736735
\keyword{constexpr} \\
736+
\columnbreak
737+
\keyword{constinit} \\
737738
\keyword{const_cast} \\
738739
\keyword{continue} \\
739740
\keyword{co_await} \\
@@ -749,8 +750,8 @@
749750
\keyword{enum} \\
750751
\keyword{explicit} \\
751752
\keyword{export} \\
752-
\columnbreak
753753
\keyword{extern} \\
754+
\columnbreak
754755
\keyword{false} \\
755756
\keyword{float} \\
756757
\keyword{for} \\
@@ -766,9 +767,9 @@
766767
\keyword{noexcept} \\
767768
\keyword{nullptr} \\
768769
\keyword{operator} \\
769-
\columnbreak
770770
\keyword{private} \\
771771
\keyword{protected} \\
772+
\columnbreak
772773
\keyword{public} \\
773774
\keyword{register} \\
774775
\keyword{reinterpret_cast} \\
@@ -783,10 +784,10 @@
783784
\keyword{struct} \\
784785
\keyword{switch} \\
785786
\keyword{template} \\
786-
\columnbreak
787787
\keyword{this} \\
788788
\keyword{thread_local} \\
789789
\keyword{throw} \\
790+
\columnbreak
790791
\keyword{true} \\
791792
\keyword{try} \\
792793
\keyword{typedef} \\

source/preprocessor.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,7 @@
16851685
\defnxname{cpp_char8_t} & \tcode{201811L} \\ \rowsep
16861686
\defnxname{cpp_conditional_explicit} & \tcode{201806L} \\ \rowsep
16871687
\defnxname{cpp_constexpr} & \tcode{201907L} \\ \rowsep
1688+
\defnxname{cpp_constinit} & \tcode{201907L} \\ \rowsep
16881689
\defnxname{cpp_coroutines} & \tcode{201902L} \\ \rowsep
16891690
\defnxname{cpp_decltype} & \tcode{200707L} \\ \rowsep
16901691
\defnxname{cpp_decltype_auto} & \tcode{201304L} \\ \rowsep

0 commit comments

Comments
 (0)