Differences between keywords constexpr, consteval and constinit
C++20 新增了两个 const 相关的关键字,于是当前存在四个相似的关键字:const,constexpr,consteval 和 constinit。
接下来分别来进行讨论。
第一,经过 const 修饰的变量具有只读属性,并且初始化发生于运行期。也就是说,若一个变量定义之后不允许被修改,就应该给它加上 const。若在一个成员函数中不修改任何成员变量,就应该在成员函数后面加上 const。但是,它也可能发生于编译期,例如以 const int 代替宏来定义数组大小。
第二,经过 constexpr 修饰的变量或是函数,既保证只读,又发生于编译期。然而,只有在参数是常量,和显式地以其返回值来初始化一个编译期常量时,它修饰的函数才会一定发生于编译期。如:
constexpr int sqr(int n) {
return n * n;
}
int main() {
// compile time
static_assert(sqr(10) == 100);
// compile time
int array[sqr(10)];
// compile time
constexpr int res = sqr(10);
// compile time or run time
int tmp = sqr(10);
// run time
int a = 10;
int tmp2 = sqr(a);
}
此处,最后两个都可能发生于运行期。
第三,consteval 用于创建一个 immediate function(立即函数),immediate function 的每次调用都会创建一个编译期常量。经过该关键字修饰的函数会自动 inline,这个函数中不能包含 static 数据,或是 try、goto、new 这种指令,也不能调用或使用非常量的函数与数据。所以,简单来说,经过 consteval 修饰的函数必定会在编译期解析。
一个小例子:
int sqr1(int n) {
return n * n;
}
constexpr int sqr2(int n) {
return n * n;
}
consteval int sqr3(int n) {
return n * n;
}
int main() {
//constexpr int res1 = sqr1(10); // error! run time
constexpr int res2 = sqr2(10); // compile time
constexpr int res3 = sqr3(10); // compile time
int a = 10;
int res4 = sqr1(a); // run time
int res5 = sqr2(a); // run time
//int res6 = sqr3(a); // error! compile time
}
这里有三个版本的 sqr 函数,sqr1() 无任何修饰,所以只能在运行期调用;sqr2() 以 constexpr 修饰,可能发生于运行期,也可能发生于编译期,取决于传入的参数;sqr3() 以 consteval 修饰,所以必定发生于编译期。因此,企图用运行期函数(sqr1)的返回值来初始化编译期常量,或是企图用非常量作为参数去调用编译期函数(sqr3),都将以失败告终。
第四,constinit 可以确保变量初始化于编译期,这些变量须得处于静态存储区或是线程存储期。静态存储区的变量,指的是全局变量、static 变量或 static 成员变量;线程存储区的变量,指的是以 thread_local 修饰的变量,就是和线程生命期绑定的局部变量。举个例子:
constexpr int val1 = 100;
constinit int val2 = 100;
int main() {
//std::cout << "++val1 " << ++val1 << '\n'; // error
std::cout << "++val2 " << ++val2 << '\n';
constexpr auto val3 = 100;
//constinit auto val4 = 100; // error
constinit thread_local auto val5 = 100;
}
如第 10 行所示,不能用 constinit 修饰非静态的局部变量。
以 constexpr 和 constinit 修饰的变量都存在于编译期,不同之处在于,constexpr 带有只读属性,而 constinit 没有。
最后,画张图来总结一下。

这里有两个维度,横向是运行时期,纵向是读写权限。关键字的作用就是来限定变量和函数在这两个维度的表现。由于四个关键字不是同一时间引入的,所以 const 和 constexpr 有交叉部分(图中没体现)。也就是说,const 也可以表现编译期,constexpr 也可以表现运行期。而 C++20 新加的两个关键字,分别用于限定非只读的编译期变量和函数,变量必须存在于静态存储区或线程存储区,函数必须是 immediate function。