const vs. constexpr vs. consteval vs. constinit — pick the right one.
C++Level: IntermediateCompile-Time Programming
const vs. constexpr vs. consteval vs. constinit — pick the right one.
Hints
- Identify types, object lifetimes and compile-time decisions.
- Separate language guarantees from a particular compiler's behavior.
Answer
constexpr Functions
:Source: src/constexpr/constexpr-function
The constexpr specifier declares that a function can be evaluated at
compile time when called with constant expressions. This enables the compiler
to compute results during compilation, eliminating runtime overhead entirely.
When called with non-constant arguments, the function executes at runtime
like a normal function. This dual behavior makes constexpr functions
versatile for both compile-time and runtime use.
#include <chrono>
#include <iostream>
constexpr long fib(long n) {
return (n < 2) ? n : fib(n - 1) + fib(n - 2);
}
int main() {
// Runtime evaluation
auto start = std::chrono::system_clock::now();
long r1 = fib(40);
std::chrono::duration<double> d1 = std::chrono::system_clock::now() - start;
std::cout << "Runtime: " << d1.count() << "s\n";
// Compile-time evaluation
start = std::chrono::system_clock::now();
constexpr long r2 = fib(40);
std::chrono::duration<double> d2 = std::chrono::system_clock::now() - start;
std::cout << "Compile-time: " << d2.count() << "s\n";
}
$ g++ -std=c++17 -O3 a.cpp && ./a.out
Runtime: 0.268229s
Compile-time: 8e-06s
consteval: Immediate Functions (C++20)
:Source: src/constexpr/consteval
C++20 introduced consteval to declare immediate functions that must be
evaluated at compile time. Unlike constexpr, which allows runtime evaluation
when arguments are not constant, consteval functions produce a compilation
error if called with non-constant arguments. This guarantees that the function
never generates runtime code, making it useful for compile-time-only utilities
like string hashing or lookup table generation.
#include <iostream>
consteval int square(int x) { return x * x; }
int main() {
constexpr int a = square(4); // OK: compile-time evaluation
std::cout << a << "\n"; // Output: 16
// int b = 5;
// int c = square(b); // Error: b is not a constant expression
}
Comparison of constexpr vs consteval:
constexpr int ce_square(int x) { return x * x; }
consteval int cv_square(int x) { return x * x; }
int main() {
int runtime_val = 5;
int a = ce_square(runtime_val); // OK: runtime evaluation
// int b = cv_square(runtime_val); // Error: must be compile-time
constexpr int c = ce_square(5); // OK: compile-time
constexpr int d = cv_square(5); // OK: compile-time
}
constinit: Constant Initialization (C++20)
:Source: src/constexpr/constinit
C++20 also introduced constinit to ensure a variable is initialized at
compile time, avoiding the static initialization order fiasco. Unlike
constexpr, a constinit variable is not const and can be modified at
runtime. It only guarantees that initialization happens at compile time,
preventing issues where static variables in different translation units
depend on each other's initialization order.
#include <iostream>
constexpr int compute() { return 42; }
constinit int global = compute(); // Initialized at compile time
int main() {
std::cout << global << "\n"; // Output: 42
global = 100; // OK: constinit doesn't mean const
std::cout << global << "\n"; // Output: 100
}
Source and licence: C/C++ Interview Cheatsheet — Chang Ning Tsai · MIT