| Document Number: | P1143r1 |
| -----------------|-------------------------------------------------|
| Date: | 2019-01-21 |
| Project: | Programming Language C++, Evolution |
| Revises: | P1143r0 |
| Reply to: | eric@efcs.ca |
# Adding the `constinit` keyword
```c++
const char *g() { return "dynamic initialization"; }
constexpr const char *f(bool p) { return p ? "constant initializer" : g(); }
constinit const char *c = f(true); // OK.
constinit const char *d = f(false); // ill-formed
```
## Introduction
We're all familar with the Static Initialization Order Fiasco. Static storage duration variables with dynamic
initializers cause hard-to-find bugs caused by the indeterminate order of dynamic initialization.
However, static variables with constant initializers avoid these pitfalls by causing the initialization to take
place at compile time. These variables can be safely used during dynamic initialization across translation units.
Unfortunatly the rules for constant initialization are complex and change dialect to dialect. This makes it
non-obvious what form of initialization is taking place simply by inspecting the code. We need a way to
enforce that constant initialization is truely taking place. It’s important to be able to fail fast instead
of silently falling back on dynamic initialization.
This paper proposes the `constinit` keyword.
The keyword can be applied to variable declarations with static storage duration. It requires that the variable have a "constant initializer". For example:
```c++
// Compiled as C++14
struct T {
constexpr T(int) {}
~T(); // non-trivial
};
constinit T x = {42}; // Initialization OK. Doesn't check destructor.
constinit T y = 42; // error: variable does not have a constant initializer
// copy initialization is not a constant expression on a non-literal type in C++14.
```
## Open Questions
### Applying `constinit` to declarations or just definitions?
`constinit` communicates the intention of the programmer to both the compiler and other programmers.
In order for the compiler to inforce the intent, the declaration need only be present on the definition,
and not any previous declarations. However, to express the intent to other programmers there is value in
allowing the attribute to appear on non-defining declarations. For example:
```c++
// Foo.h
struct Foo {
constinit static int x;
};
// Foo.cpp
int Foo::x = 42;
```
However, there is another case where a variable declaration is not a definition, and that's when `extern` is specified.
In this case the `extern` variable declaration may not be available to the compiler when checking the definition, and so
it has no way of knowing that it was, at one point, declared with `constinit`. This causes the keyword to be silently
ignored.
For these reasons this proposal disallows `constinit` from being mixed with `extern` unless the declaration is a definition.
For example:
```c++
constinit extern int x = 42; // OK
constinit extern int y; // ill-formed.
```
This issue also affects redundant redeclarations of `constexpr` static data members. For example:
```c++
struct Foo {
static constexpr int x = 42;
};
constinit constexpr int Foo::x; // OK?
```
In the above case, the addition of `constinit` to a `constexpr` variable is meaningless. Additionally, allowing a redeclaration to add `constinit` after the variable is defined would be problematic (though in this particular case `constexpr` makes it moot).
## Previous Discussions
### ``constinit`` as an attribute.
in r0 of this paper `constinit` was proposed as an attribute. When this idea was presented, there was general concencious that this feature would be better suited as a keyword. First, `constinit` enforces correctness and if compilers were allowed to ignore it as they can attributes, it would allow "ill-formed" programs to compile. Second, there was some discussion about the behavior of `[[constinit]]` being out-of-scope for attributes (I don't believe this to be the case).
## Wording
Modify `[dcl.spec]` as described below.
#### 9.1 Specifiers [dcl.spec]
1. The specifiers that can be used in a declaration are:
...
constinit
2.Each decl-specifier shall appear at most once in a complete decl-specifier-seq, except that long may appear twice. The constexpr and consteval decl-specifiers shall not both appear in a decl-specifier-seq.At most one of the `constexpr`, `consteval`, and `constinit` keywords shall appear in a decl-specifier-seq.
#### 9.1.X The `constinit` specifier [dcl.constinit]
Add the new section under `[dcl.spec]` with the following wording.
1. The `constinit` specifier shall be applied only to declaration of a variable with static or thread storage duration.
2. The attribute may not appear on a variable declaration unless the declaration is a definition or declares a static data member in a class definition. [*Note*: This ensures that the presence or absence of the attribute can be determined at the point of definition --- *end note*]
3. A variable declared without the `constinit` specifier can later be redeclared with `constinit` and vice-versa.
4. If a variable declared with the `constinit` specifier has *dynamic initialization* (`[basic.start.dynamic]`), the program is ill-formed. [*Note*: Otherwise the variable is fully initialized during *static initialization* using either *constant initialization* or *zero initialization* --- *end note*]
5. [*Example*:
```c++
const char *g() { return "dynamic initialization"; }
constexpr const char *f(bool p) { return p ? "constant initializer" : g(); }
constinit const char *c = f(true); // OK.
constinit const char *d = f(false); // ill-formed
```
--- *end example*]
## References
* [Clang's `[[require_constant_initialization]]` documentation](https://clang.llvm.org/docs/AttributeReference.html#require-constant-initialization-clang-require-constant-initialization)
* [A Clang implementation of the r0 proposal](https://github.com/efcs/clang/tree/fix-const-init)
* [Definition of *Constant Initialization* from `[basic.start.static]`](http://eel.is/c++draft/basic.start#static-2)