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


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.


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 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 is:

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.



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.


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.


Rainer D 6 P2 540x540Modernes C++ Mentoring

Stay informed about my mentoring programs.



Subscribe via E-Mail.

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(10) == 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.


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: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, and Ann Shatoff.


Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, and Slavko Radman.



My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small


My special thanks to PVS-Studio PVC Logo


My special thanks to logo


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

Bookable (Online)


Standard Seminars (English/German)

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

  • C++ - The Core Language
  • C++ - The Standard Library
  • C++ - Compact
  • C++11 and C++14
  • Concurrency with Modern C++
  • Design Pattern and Architectural Pattern with C++
  • Embedded Programming with Modern C++
  • Generic Programming (Templates) with C++


  • Clean Code with Modern C++
  • C++20

Contact Me

Modernes C++,



+2 #1 Scheff 2020-12-03 07:29

`static_assert(sqrRunOrCompileTime(100) == 100);`

should always produce a compiler error, shouldn't it?

Not because of compile-/run-time issues but because 100 * 100 != 100. ;-)
+1 #2 a b 2021-07-07 21:56
shouldn't this:
int incrementMe(int val){ return ++val;}
be this:
int incrementMe(int &val){ return ++val;}
Pass by reference instead of pass by value


Stay Informed about my Mentoring


English 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

Interactive Course: The All-in-One Guide to C++20

Subscribe to the newsletter (+ pdf bundle)

All tags

Blog archive

Source Code


Today 3103

Yesterday 5317

Week 3103

Month 147274

All 11628428

Currently are 214 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments