💡 TL;DR:The complete C++ const family in one tour: const vs constexpr vs constinit, consteval and const-qualified members, plus how C++23 deducing this collapses four const/ref overloads into one function.
Last updated:2026-10-13
The Complete C++ const Family¶
Outline
- const
- constexpr
- C++20
- consteval
- constinit
Note: all code examples in this article have been posted to the Knowledge Planet.
1. const¶
const is one of the earliest keywords we meet when learning C++/C. It is usually used as a cv-qualifier to indicate that an object is immutable. It does not guarantee compile-time evaluation; when exactly the evaluation happens is up to the compiler. For example:
const can sometimes be used in compile-time constant expressions, but there are exceptions, for example:
const double count_1 = 3.3;
std::array<double, static_cast<int>(count_1)> moreDoubles1 {1.1, 2.2, 3.3};
This produces an error: the value of 'count_1' is not usable in a constant expression
But this is fine:
The essence is compile-time expression evaluation, and so C++11 introduced constexpr.
2. constexpr¶
constexpr can be used to specify that a variable's value may appear in a constant expression; here we only need to change const to constexpr in the earlier example for it to work.
constexpr double count_2 = 3.3;
std::array<double, static_cast<int>(count_2)> moreDoubles2 {1.1, 2.2, 3.3};
constexpr does not necessarily execute only at compile time. Its essential meaning is that it can be computed at compile time, but if a constexpr variable or function is used at run time, it can also execute at run time.
For example, result1 and result2 are completely different: result1 is computed at compile time, while result2 is computed at run time. A question to leave you with: do you know why?
So a function specified as constexpr is not necessarily computed at compile time.
constexpr int compileTimeSum(int a, int b) {
return a + b;
}
int main() {
constexpr int result1 = compileTimeSum(3, 5);
int x = 10;
int result2 = compileTimeSum(x, 20);
return 0;
}
Then who can guarantee that? Along comes C++20.
3. C++20¶
3.1 consteval¶
consteval solves the problem above: it can only be used on functions and forces every call to happen at compile time. If we now change constexpr to consteval above, what happens to result2?
It errors out: call to consteval function 'compileTimeSum(x, 20)' is not a constant expression
consteval int compileTimeSum(int a, int b) {
return a + b;
}
int main() {
constexpr int result1 = compileTimeSum(3, 5);
int x = 10;
int result2 = compileTimeSum(x, 20);
return 0;
}
If consteval is used on a variable, it also errors: error: a variable cannot be declared 'consteval'.
3.2 constinit¶
constinit forces static or thread-local variables to undergo constant initialization.
- Cannot be used together with constexpr or consteval
- A non-type specifier whose role is that of an assert
- Guarantees that the variable is statically initialized
- Can only be used with static or thread_local
For example, the following fails to compile: error: 'constinit' can only be applied to a variable with static or thread storage duration
constinit int a = 100;
int main() {
++a;
constinit auto b = 100; // error
// b has thread storage duration
constinit thread_local auto b = 100;
}
C++23 deducing this: Write the const/ref Overloads Once¶
Every article in the const family eventually bumps into the same wall: a getter that should behave correctly for const and non-const objects, for lvalues and rvalues, needs up to four near-identical overloads — and they will drift apart the day someone edits only one of them.
C++23 "deducing this" (explicit object parameter, proposal P0847) removes the duplication. The member function declares its own object as an explicit parameter; the compiler deduces its type together with constness and value category, so one template covers all four cases:
class Widget {
int value_{42};
public:
// Before C++23 you needed up to four overloads:
// int& get() & { return value_; }
// int get() const& { return value_; }
// int&& get() && { return std::move(value_); }
// ...
// C++23 deducing this — one template covers them all:
template <typename Self>
auto&& get(this Self&& self) {
return std::forward<Self>(self).value_;
}
};
When get() is called on a const lvalue, Self deduces as const Widget&; on an rvalue, as Widget&& and std::forward moves. The const family knowledge above still decides what should be const — deducing this just removes the copy-paste.
The feature also does two jobs CRTP used to do — mixing in behavior and deducing the derived type — without the base-class template boilerplate. Compiler support: GCC 14+, Clang 18+, MSVC since VS 2022 17.2.
FAQ¶
Can deducing this replace const overloads?¶
Yes for the common case. A single template on Self&& with std::forward behaves correctly for &, const&, && and const&&. You still write separate overloads when the behavior truly differs per qualification, which is rare.
Which compilers support deducing this?¶
GCC 14 and later, Clang 18 and later, and MSVC since Visual Studio 2022 17.2. It is a C++23 feature, so compile with -std=c++23 (or c++2b on older drivers).
Is deducing this the same as CRTP?¶
It overlaps with CRTP: both deduce the most-derived type. Deducing this needs no base-class template, works on lambdas (recursive lambdas!), and keeps the class hierarchy clean; CRTP still fits when the mixin must be a reusable base class.
Related Topics¶
- C++ Pure Virtual Functions and Abstract Classes | C++那些事
- C++ assert | C++那些事
- C++ Bit Fields | C++那些事
- Implementing C++ Polymorphism in C | C++那些事
- C++ const Explained: Constants, const Pointer vs Pointer to const | C++那些事
- C++ Dereference Operator * | C++那些事
- C++ Initialization and Assignment | C++那些事
- Ways to Return Multiple Values in C++ | C++那些事
- C++ mutable Keyword | C++那些事
- C++ restrict and Compiler Optimization | C++那些事