Skip to content

Commit 77f7730

Browse files
committed
补充左移右移未定义行为的问题
1 parent 2fad1b8 commit 77f7730

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

docs/undef.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,18 @@ i << 0; // 可以
314314
i << -1; // 错!
315315
```
316316

317-
不过一个例外是[整数提升](https://zh.cppreference.com/w/cpp/language/implicit_conversion#.E6.95.B4.E6.95.B0.E6.8F.90.E5.8D.87)
317+
但是你还需要考虑一件事情:**隐式转换**,或者直接点说:**整数提升**
318+
319+
- 在 C++ 中算术运算符不接受小于 int 的类型进行运算。如果你觉得可以,那只是隐式转换,整形提升了。
320+
321+
```cpp
322+
std::uint8_t c{ '0' };
323+
using T1 = decltype(c << 1); // int
324+
```
325+
326+
即使移位大于等于 8 也不成问题。
327+
328+
---
318329
319330
对于有符号整数,左移还不得破坏符号位
320331

0 commit comments

Comments
 (0)