In C++20 we now have constinit
. constexpr
and consteval
.
I can now guarantee that a static variable is initialized by the result of a constexpr
or consteval
function by using constinit
. OK
I also can guarantee that a stack variable is initialized with the result of a consteval
function executed in compile time.
But how can I force to run a constexpr
function to calculate a result in compile time to init a variable on stack?
// Lets assume that we have a much more complex function so that the compiler will not
// always decide to compile time evaluation automatically
constexpr int func( int i )
{
return i+2;
}
int main()
{
??? int i = func(8);
...
i = 9;
}
If we use constexpr
the variable is implicitly const and constinit
is not allowed here. Any chance to initialize this var with the compile time evaluated result of a constexpr
function without making it const? I am simply wondering why constinit
is limited to static variables. For my it makes not much sense.