Skip to content

Commit

Permalink
chore(lang/const.md): 修改对 const constexpr 的直观描述用语 (OI-wiki#6115)
Browse files Browse the repository at this point in the history
* Update const.md

* Update const.md

* style: format markdown files with remark-lint

---------

Co-authored-by: 24OI-bot <[email protected]>
  • Loading branch information
typed-sigterm and 24OI-bot authored Feb 2, 2025
1 parent 8138ef4 commit 729cd9a
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/lang/const.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ int main() {
}
```

## 常量表达式 constexpr(C++11)
## 常量表达式 `constexpr`(C++11)

常量表达式是指编译时能计算出结果的表达式,`constexpr` 则要求编译器能在编译时求得函数或变量的值。

编译时计算能允许更好的优化,比如将结果硬编码到汇编中,消除运行时计算开销。与 `const` 的带来的优化不同,当 `constexpr` 修饰的变量满足常量表达式的条件,就强制要求编译器在编译时计算出结果而非运行时。

???+ note " 实际上把 `const` 理解成 **"readonly"**`constexpr` 理解成 **"const"**,这样更加直观 "
???+ note " 更直观的理解是把 `const` 理解成「只读」`constexpr` 理解成「不可变」"
```cpp
constexpr int a = 10; // 直接定义常量

Expand Down

0 comments on commit 729cd9a

Please sign in to comment.