c++c++20constexprconstevalconstinit

how to guarantee initilization of a stack variable with a compile time constant


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.


Solution

  • I think it is better to use consteval function, but if you cannot change it, you can simply use a temporary variable which will surely optimize later:

    constexpr int func( int i )
    {
        return i+2;
    }
    
    int main()
    {
         constexpr int i1 = func(8);
         auto i2 = i1;
         i2 = 9;
    }
    

    Although you may not like this method, I think it works without any problem....

    You can also use something like this(Based on StoryTeller idea):

    template<typename T> constexpr std::remove_const_t<T> const_eval(T&& res) { return res; }
    

    which support returning references from method too.