With C++20, we get two new keywords: consteval and constinit. consteval produces a function executed at compile-time, and constinit guarantees that a variable is initialized at compile-time.
When you read my previous short description of consteval and constinit you may have the impression that both specifiers are pretty 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 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 specifiers in a declaration. An immediate function (consteval) is implicit inline and must fulfill a constexpr function’s requirements.
The requirements for 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.
5 is a constant expression and can be used as an argument for the function sqr (1).
The same holds for 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 program’s output.
constinit can be applied to variables with static 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 their 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 that this kind of variable (static storage duration or thread storage duration) is initialized at compile-time.
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.
The following program consteval.cpp has three versions of a square function.
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 an 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.
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 the optimization level. This observation does not hold for line (4). A consteval function is always executed at compile-time.
In the following program constexprConstinit.cpp, I compare const, constexpr, and constint.
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.
Initialization of static variables in different translation units has a severe 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, 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, Ann Shatoff, Rob North, and Bhavith C Achar.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
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++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,