constinit
keywordconst 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
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:
// 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.
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).
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
specifier need only be present on the initializing declaration, and not any
other declarations. However, to express the intent to other programmers there
is value in allowing the constinit
specifier to appear on non-defining
declarations. For example:
// Foo.h
struct Foo {
constinit static int x;
};
// Foo.cpp
int Foo::x = 42;
constinit
can also be useful to compilers for non-initializing declarations
of thread_local
variables:
extern thread_local constinit x;
int f() { return x; }
Without constinit
, runtime code must be executed to perform a check of a
guard variable and conditionally initialize x
each time it is used. (Other
techniques exist, but this approach is common.) If the variable is known to
have constant initialization, this can be avoided.
After EWG discussion, constinit
is permitted on any declarations of a
variable; if it is present on some declaration but not on the initializing
declaration, the program is ill-formed, no diagnostic required.
Modify [dcl.spec]
as described below.
Add constinit
to Table 5 (Keywords).
The specifiers that can be used in a declaration are:
decl-specifier:
...
constinit
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.
constinit
specifier [dcl.constinit]Add the new subclause under [dcl.spec]
with the following wording.
constinit
specifier shall be applied only to a declaration of a variable with static or thread storage duration. If the specifier is applied to any declaration of a variable, it shall be applied to the initializing declaration. No diagnostic is required if no constinit
declaration is reachable at the point of the initializing declaration.constinit
specifier has dynamic initialization ([basic.start.dynamic]), the program is ill-formed. [Note: The constinit
specifier ensures that the variable is initialized during static initialization ([basic.start.static]). --- end note][Example:
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]
Add a feature test macro __cpp_constinit
with a suitable value to Table 17 (Feature-test macros).
Add a bullet to the "New keywords" change in paragraph 2:
- The constinit keyword is added to prevent unintended dynamic initialization.