Two new Keywords in C++20: consteval and constinit

Contents[Show]

With C++20 we get two new keywords: consteval and constinit. consteval produces a function that is executed at compile-time and constinit guarantees that a variable is initialized at compile-time.

 TimelineCpp20

When you read my previous short description about consteval and constinit you may have the impression that both specifiers are quite similar to constexpr. To make it short, you are right. Before I compare the keywords consteval, constinit, constexpr, and good old const, I have to introduce the new specifiers consteval and constinit.

consteval

consteval int sqr(int n) {
    return n * n;
}

 

consteval creates a so-called immediate function. Each invocation of an immediate function creates a compile-time constant. To say it more directly. A consteval (immediate) function is executed at compile-time.

consteval cannot be applied to destructors or functions which allocate or deallocate. You can only use at most one of consteval, constexpr, or constinit specifier in a declaration. An immediate function (consteval) is implicit inline and has to fulfill the requirements of a constexpr function.

The requirements to a constexpr function in C++14 and, therefore, a consteval function are:

A constexpr function can

  • have conditional jump instructions or loop instructions.
  • have more than one instruction.
  • invoke constexp functions. A consteval function can only invoke a constexpr function but not the other way around.
  • have fundamental data types that have to be initialized with a constant expression.

constexpr functions can not have static or thread_local data. Neither can they have a try block nor a goto instruction.

The program constevalSqr.cpp applies the consteval function sqr.

// constevalSqr.cpp

#include <iostream>

consteval int sqr(int n) {
    return n * n;
}

int main() {
    
    std::cout << "sqr(5): " << sqr(5) << std::endl;     // (1)
    
    const int a = 5;                                    // (2)
    std::cout << "sqr(a): " << sqr(a) << std::endl;     

    int b = 5;                                          // (3)
    // std::cout << "sqr(b): " << sqr(b) << std::endl; ERROR

}

 

5 is a constant expression and can be used as an argument for the function sqr (1).

The same holds for the variable a (2). A constant variable such as a is usable in a constant expression when it is initialized with a constant expression.

b (3) is not a constant expression. Consequently, the invocation of sqr(b) is not valid.

Thanks to the brand-new GCC11 and the Compiler Explorer, here is the output of the program.

constevalSqr

constinit

constinit can be applied to variables with static storage duration or thread storage duration.

  • Global (namespace) variables, static variables, or static class members have static storage duration. These objects are allocated when the program starts and deallocated when the program ends.
  • thread_local variables have thread storage duration. Thread local data is created for each thread that uses this data. thread_local data exclusively belongs to the thread. They are created at its first usage and its lifetime is bound to the lifetime of the thread it belongs to. Often thread local data is called thread local storage.

constinit ensures for this kind of variable (static storage duration or thread storage duration) that they are initialized at compile-time.

 

// constinitSqr.cpp

#include <iostream>

consteval int sqr(int n) {
    return n * n;
}

    constexpr auto res1 = sqr(5);                  
    constinit auto res2 = sqr(5);                 

int main() {

    std::cout << "sqr(5): " << res1 << std::endl;
    std::cout << "sqr(5): " << res2 << std::endl;
   
    constinit thread_local auto res3 = sqr(5);     
    std::cout << "sqr(5): " << res3 << std::endl;

}

 

res1 and res2 have static storage duration. res3  has thread storage duration.

constinitSqr

Now it's time to write about the differences between const, constexpr, consteval, and constinit. Let me first write about function execution and then about variable initialization.

Function Execution

 The following program consteval.cpp has three versions of a square function.

// consteval.cpp

#include <iostream>

int sqrRunTime(int n) {
    return n * n;
}

consteval int sqrCompileTime(int n) {
    return n * n;
}

constexpr int sqrRunOrCompileTime(int n) {
    return n * n;
}

int main() {

    // constexpr int prod1 = sqrRunTime(100); ERROR (1)
    constexpr int prod2 = sqrCompileTime(100);
    constexpr int prod3 = sqrRunOrCompileTime(100);
    
    int x = 100;
    
    int prod4 = sqrRunTime(x); 
    // int prod5 = sqrCompileTime(x); ERROR (2)
    int prod6 = sqrRunOrCompileTime(x);

}

 

As the name suggests it. The ordinary function sqrRunTime runs at run-time; the consteval function sqrCompileTime runs at compile-time; the constexpr function sqrRunOrCompileTime can run at compile-time or run-time. Consequently, asking for the result at compile-time with sqrRunTime (1) is an error or using a non-constant expression as argument for sqrCompileTime (2) is an error.

The difference between the constexpr function sqrRunOrCompileTime and the consteval function sqrCompileTime is, that sqrRunOrCompileTime has only to run at compile-time when the context requires compile-time evaluation.

static_assert(sqrRunOrCompileTime(100) == 100);                   // compile-time (1)
int arrayNewWithConstExpressioFunction[sqrRunOrCompileTime(100)]; // compile-time (1)
constexpr int prod = sqrRunOrCompileTime(100);                    // compile-time (1)

int a = 100;
int runTime = sqrRunOrCompileTime(a);                 // run-time (2)

int runTimeOrCompiletime = sqrRunOrCompileTime(100);  // run-time or compile-time (3)

int allwaysCompileTime = sqrCompileTime(100);         // compile-time (4)

 

The first three lines (1) require compile-time evaluation. Line (2) can only be evaluated at run-time because a is not a constant expression. The critical line is (3). The function can be executed at compile-time or run-time. If it is executed at compile-time or run-time may depend on the compiler or on the optimization level. This observation does not hold for line (4). A consteval function is always executed at compile-time.

Variable Initialization

 In the following program constexprConstinit.cpp, I compare const, constexpr, and constint.

// constexprConstinit.cpp

#include <iostream>

constexpr int constexprVal = 1000;
constinit int constinitVal = 1000;

int incrementMe(int val){ return ++val;}

int main() {

    auto val = 1000;
    const auto res = incrementMe(val);                                      // (1)                         
    std::cout << "res: " << res << std::endl;
    
// std::cout << "res: " << ++res << std::endl; ERROR (2) // std::cout << "++constexprVal++: " << ++constexprVal << std::endl; ERROR (2) std::cout << "++constinitVal++: " << ++constinitVal << std::endl; // (3) constexpr auto localConstexpr = 1000; // (4) // constinit auto localConstinit = 1000; ERROR }

 

Only the const variable  (1) is initialized at run-time. constexpr and constinit variables are initialized at compile-time.

constinit (3) does not imply constness such as const (2), or  constexpr(2).  A  constexpr (4) or const (1) declared variable can be created as a local but a constinit declared variable not.

constexprConstinit

What's next?

Initialization of static variables in different translation unit has a serious issue: If the initialization of one static depends on another static, it is not defined, in which sequence they are initialized. To make it short, my next post is about the Static Initialization Order Fiasco and how you can solve it with constinit.

 

 

Thanks a lot to my Patreon Supporters: Meeting C++, Matt Braun, Roman Postanciuc, Venkata Ramesh Gudpati, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Darshan Mody, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Wolfgang Gärtner, Jon Hess, Christian Wittenhorst, Louis St-Amour, Stephan Roslen, Venkat Nandam, Jose Francisco, Douglas Tinkham, Lakshman, Kuchlong Kuchlong, Avi Kohn, Serhy Pyton, Robert Blanch, Kuma [], and Truels Wissneth.

 

Thanks in particular to: Bitwyre Technologies

 

Thanks in particular to:   crp4

 

Seminars

I'm happy to give online-seminars or face-to-face seminars world-wide. Please call me if you have any questions.

Bookable Seminars (Online)

Standard Seminars 

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

Contact Me

Modernes C++,

RainerGrimmSmall

Tags: C++20

My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code

Visitors

Today 3411

All 4347820

Currently are 293 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments