constexpr C++ · 关键字
编译期常量说明符,表示变量或函数的值/结果可以在编译期间确定,能用于需要常量表达式的地方。
怎么用
constexpr 类型 名字 = 表达式;
小例子
constexpr int S = 1 + 2;
int arr[S] = {};
std::cout << S;
编译结果
3
提醒:C++11 起可用;constexpr 函数在 C++14 后才能有局部变量和多个 return
在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← C++全部词条