Skip to content

更正有符号整数左移的相关未定义行为描述 #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions docs/undef.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,15 @@ using T1 = decltype(c << 1); // int

---

对于有符号整数,左移还不得破坏符号位
C++20 前,有符号整数负值不能左移,而左移正值不能导致结果超出对应的无符号整数类型范围。

```cpp
int i = 0;
i << 1; // 可以
i << 31; // 错!
int i = 1;
i << 31; // 可以
int j = 42;
j << 31; // C++20 前错! C++20 起可以
int k = -1;
k << 0; // C++20 前错! C++20 起可以
unsigned int u = 0;
u << 31; // 可以
```
Expand Down