0
0
CppComparisonBeginner · 4 min read

Const vs constexpr in C++: Key Differences and Usage

const declares variables whose values cannot change after initialization, but they may be set at run-time. constexpr requires the value to be known and fixed at compile-time, enabling optimizations and use in compile-time contexts.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of const and constexpr in C++.

Factorconstconstexpr
Value InitializationCan be run-time or compile-timeMust be compile-time constant
Usage ContextRead-only variables, pointers, referencesCompile-time constants, usable in constant expressions
Function QualificationCan be used with functions but no compile-time guaranteeFunctions must be literal and evaluable at compile-time
PerformanceNo guaranteed compile-time evaluationEnables compiler optimizations and constant folding
MemoryMay occupy memory at run-timeOften optimized out, no run-time memory needed
Introduced InSince early C++ versionsSince C++11
⚖️

Key Differences

const means the variable's value cannot be changed after it is set, but the value can be determined at run-time. For example, a const variable can be initialized from user input or a function call that runs when the program executes.

constexpr requires the value to be known at compile-time. This means the compiler can evaluate the value during compilation, allowing it to be used in places where only compile-time constants are allowed, like array sizes or template parameters.

Functions marked constexpr must be simple enough for the compiler to evaluate them at compile-time, while const functions do not have this restriction. This makes constexpr more powerful for metaprogramming and optimization but also more restrictive in what it can do.

💻

Const Example

cpp
#include <iostream>

int getValue() {
    return 10;
}

int main() {
    const int x = getValue(); // run-time constant
    std::cout << "const x = " << x << std::endl;
    return 0;
}
Output
const x = 10
↔️

Constexpr Equivalent

cpp
#include <iostream>

constexpr int getValue() {
    return 10;
}

int main() {
    constexpr int x = getValue(); // compile-time constant
    std::cout << "constexpr x = " << x << std::endl;
    return 0;
}
Output
constexpr x = 10
🎯

When to Use Which

Choose const when the value is fixed after initialization but may only be known at run-time, such as configuration values or results from functions that run during execution.

Choose constexpr when you want the value to be computed at compile-time for better performance, to use in compile-time contexts like array sizes, or to enable compiler optimizations.

In short, use constexpr for true constants and compile-time computations, and const for read-only variables that may depend on run-time information.

Key Takeaways

const variables are read-only but can be initialized at run-time.
constexpr variables must be initialized with compile-time constant expressions.
constexpr enables compile-time evaluation and optimizations.
Use const for fixed run-time values, constexpr for compile-time constants.
constexpr functions allow computations during compilation.