const and constexpr
const means this object will not be mutated through this name after initialisation. constexpr means the value (or the function) can be evaluated at compile time, so you can use it as an array bound, a template argument, or a constant expression.
Detailed Explanation
You have a tax rate that must not change after you read it from config, and a table size that must be known while the compiler is still running. Those are two different jobs. const handles the first. constexpr handles the second, and also the first.
const is a promise about mutation. const int n = 4; cannot be assigned later. const std::string& name means the function will not call non-const methods on that string. On a member function, the const after the parameter list (int size() const) means the function will not modify *this, so it is legal to call on a const object. The compiler enforces the promise: assigning through a const name is a type error.
const is part of the type. int and const int are different. That is why you can overload on const, and why a const member function and a non-const one can share a name. It is also why const on a pointer has two positions: const T* is a pointer to const data, T* const is a const pointer.
constexpr is a promise about when the value is known. A constexpr variable must be initialised by a constant expression. A constexpr function must be written so that at least some calls can run at compile time: no uninitialised reads, no new, no throw that actually fires in that call, and (until C++20) a fairly strict body. From C++20 the rules relaxed; you can have more statements, and constexpr allocations that are deallocated by the end of the evaluation.
The payoff is concrete. constexpr int N = 16; can size a std::array<int, N>. A const int N = readConfig(); cannot, because readConfig() runs later. constexpr also lets the compiler fold work into the binary: a constexpr parser that runs on a string literal produces a table in the executable, with no startup cost.
const on a runtime value is still useful. A const std::string you build from user input cannot be used as a template argument, but it documents and enforces that the rest of the function will not rewrite the string. That is a contract for readers and for the optimiser, which may then cache a length or skip a copy.
constexpr implies const for objects. You do not write constexpr const int n = 1;. For functions, constexpr means "usable in a constant expression if the arguments are". The same function can still be called at run time with a runtime argument.
Use const by default on locals, references, and member functions that do not mutate. Use constexpr when the value is needed during compilation, or when you want the compiler to do the work ahead of time. Use consteval when the call must happen at compile time, and constinit when you need static initialisation without requiring the variable itself to be const.
Code Examples
#include <array>
#include <iostream>
constexpr int square(int n) {
return n * n; // usable at compile time or run time
}
int runtimeValue() { return 7; }
int main() {
const int rate = 10; // immutable after this line
// rate = 11; // error
constexpr int cells = square(4);
std::array<int, cells> grid{}; // size must be a constant expression
std::cout << grid.size() << '\n'; // prints: 16
int n = runtimeValue();
std::cout << square(n) << '\n'; // prints: 49 (runtime call)
}#include <iostream>
#include <string>
#include <utility>
struct User {
std::string name;
std::size_t size() const { return name.size(); } // allowed on const User
void rename(std::string n) { name = std::move(n); }
};
void print(const User& u) {
std::cout << u.size(); // ok: size() is const
// u.rename("x"); // error: rename is not const
}Common Gotchas
`const` on a pointer is easy to misplace
`const char* p` cannot change the characters. `char* const p` cannot change `p`. `const` binds left unless it is first, in which case it binds right. Read from the name outward.
A `constexpr` variable cannot depend on runtime input
`constexpr int n = argc;` does not compile. Use `const` for values computed after startup.
`constexpr` functions still run at runtime
passing a runtime `int` to `square` generates an ordinary call. There is no hidden "must be compile-time" unless you used `consteval`.
`const` is not thread-safety
a `const` method may mutate a `mutable` member or data behind a pointer. `const` means "not through this interface", not "no writes exist".