51 lines
2.0 KiB
Markdown
51 lines
2.0 KiB
Markdown
[dcl.constinit]
|
||
|
||
# 9 Declarations [[dcl]](./#dcl)
|
||
|
||
## 9.2 Specifiers [[dcl.spec]](dcl.spec#dcl.constinit)
|
||
|
||
### 9.2.7 The constinit specifier [dcl.constinit]
|
||
|
||
[1](#1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L1056)
|
||
|
||
The constinit specifier shall be applied only
|
||
to a declaration of a variable with static or thread storage duration
|
||
or to a structured binding declaration ([[dcl.struct.bind]](dcl.struct.bind "9.7 Structured binding declarations"))[.](#1.sentence-1)
|
||
|
||
[*Note [1](#note-1)*:
|
||
|
||
A structured binding declaration introduces a uniquely named variable,
|
||
to which the constinit specifier applies[.](#1.sentence-2)
|
||
|
||
â *end note*]
|
||
|
||
If the specifier is applied to any declaration of a variable,
|
||
it shall be applied to the initializing declaration[.](#1.sentence-3)
|
||
|
||
No diagnostic is required if no constinit declaration
|
||
is reachable at the point of the initializing declaration[.](#1.sentence-4)
|
||
|
||
[2](#2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L1069)
|
||
|
||
If a variable declared with the constinit specifier has
|
||
dynamic initialization ([[basic.start.dynamic]](basic.start.dynamic "6.10.3.3 Dynamic initialization of non-block variables")), the program is ill-formed,
|
||
even if the implementation would perform that initialization as
|
||
a static initialization ([[basic.start.static]](basic.start.static "6.10.3.2 Static initialization"))[.](#2.sentence-1)
|
||
|
||
[*Note [2](#note-2)*:
|
||
|
||
The constinit specifier ensures that the variable
|
||
is initialized during static initialization[.](#2.sentence-2)
|
||
|
||
â *end note*]
|
||
|
||
[3](#3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L1079)
|
||
|
||
[*Example [1](#example-1)*: const char * g() { return "dynamic initialization"; }constexpr const char * f(bool p) { return p ? "constant initializer" : g(); }constinit const char * c = f(true); // OKconstinit const char * d = f(false); // error â *end example*]
|