const: giá trị không thay đổi sau khởi tạo — có thể tính toán lúc runtime.constexpr: giá trị (hoặc hàm) được tính tại compile-time — không tốn chi phí runtime.
const int a = some_function(); // tính lúc runtime — OK
constexpr int b = 5 * 3; // tính lúc compile-time — 15
// const int arr[a]; // lỗi — a không phải compile-time constant
int arr[b]; // OK — b là constexpr
constexpr int factorial(int n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}
constexpr int f5 = factorial(5); // = 120, tính lúc compile-timeC++14 mở rộng constexpr cho phép if, for, biến local. C++17 thêm if constexpr cho template branching.
Khi nào dùng constexpr: magic numbers trong code, kích thước mảng, template argument, tránh overhead runtime với giá trị cố định.
const: value does not change after initialisation — may be computed at runtime.constexpr: value (or function) evaluated at compile-time — zero runtime cost.
const int a = some_function(); // runtime — fine
constexpr int b = 5 * 3; // compile-time — 15
// const int arr[a]; // error — a is not a compile-time constant
int arr[b]; // OK — b is constexpr
constexpr int factorial(int n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}
constexpr int f5 = factorial(5); // = 120, computed at compile-timeC++14 expanded constexpr to allow if, for, and local variables. C++17 added if constexpr for compile-time template branching.
When to use constexpr: named constants, array sizes, template arguments, eliminating runtime overhead for fixed values.