Listen to this Post
C++ offers different specifiers with similar names but different purposes. Let’s dig a bit deeper to figure out what they are and when to use them.
constexpr
The most broad specifier applicable to variables, functions, and class methods. Used with a variable, it marks it as a constant compile-time value. That makes it possible to use such a constant in template parameter specialization or in any other compile-time expressions. For functions and member methods, `constexpr` allows them to be invoked at both compile-time and runtime. `constexpr` constructor facilitates creation of an object at compile-time. When the method will be called highly depends on the context: if parameters are compile-time values, like literals or `constexpr` variables, and the result is assigned to a `constexpr` variable, then execution is done at compile-time. But if one of the parameters is an ordinary variable or the result of some non-constexpr function, then the `constexpr` function is executed at runtime.
consteval
To enable function calls only at compile-time, C++ offers another qualifier – consteval. Invoking such a function at runtime will cause a compilation error. If the constructor of an object is marked as consteval, this object might be created only at compile-time.
constinit
`constinit` might be used only for variables. It doesn’t make a variable constant, instead, it forces the variable to be initialized with a value known at compile-time, for example, with a literal or the result of a consteval/constexpr function. That is needed because of the unspecified order of static and thread-local variable initialization. The standard states that `constinit` variables are initialized before the run of `main` and even before the initialization of ordinary `static` variables. Let’s say you can create an object as a `constinit` variable and then call methods of this object to initialize other static variables.
You Should Know:
Here are some practical examples and commands related to the article:
1. constexpr Example:
constexpr int square(int x) {
return x * x;
}
constexpr int val = square(10); // Computed at compile-time
2. consteval Example:
consteval int add(int a, int b) {
return a + b;
}
constexpr int result = add(5, 10); // Must be computed at compile-time
3. constinit Example:
constinit int globalVar = 42; // Initialized at compile-time
4. Linux Command to Compile C++ Code:
g++ -std=c++20 -o my_program my_program.cpp
5. Windows Command to Compile C++ Code:
cl /EHsc /std:c++20 my_program.cpp
6. Check C++ Version:
g++ --version
7. Run Compiled C++ Program:
./my_program
What Undercode Say:
Understanding constexpr, consteval, and `constinit` is crucial for modern C++ development, especially when working with compile-time computations and optimizations. These specifiers help in writing efficient and predictable code by ensuring certain operations are performed at compile-time, reducing runtime overhead. Always ensure your compiler supports the required C++ standard (C++20 for `consteval` and constinit) to leverage these features effectively. For further reading, refer to the C++ documentation.
References:
Reported By: Nikolai Kutiavin – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅



