C++20 constinit specifier
In this three part tutorial series, we explore the new specifier, constinit
, and consteval
added in C++20 as a core language feature and compare its subtle differences with constexpr
specifier that is available since C++11.
In the first part I explain the constinit
specifier. The most simple explaination of constinit
is that it guarantees that the variable is initialized at compile time and when the initialization is not possible we get a compilation error.
1. What does constinit
mean?
constinit
specifier can be used only with static storage duration variables to and it guarantees compile time initialization of variables. Lets see this in action. To demonstrate the examples, it would be good to know how to identify when a variable has a static storage duration.
const int gConstInt{9}; //const global variable has static storagestatic int gStaticInt{9}; //global variables with static specifier
//are same as gConstIntstatic const int gStaticConstInt{9}; //static and const together on
// a global variable is trivialint gInt{9}; //otherwise global variables have automatic
//storage duration