|
| 1 | +<div align=right> |
| 2 | + |
| 3 | + 🌎 [中文] | [English] |
| 4 | +</div> |
| 5 | + |
| 6 | +[中文]: ../../cpp14/06-digit-separators.html |
| 7 | +[English]: ./06-digit-separators.html |
| 8 | + |
| 9 | +# Digit Separators |
| 10 | + |
| 11 | +C++14 allows single quotes `'` as separators in numeric literals, improving readability without affecting the value |
| 12 | + |
| 13 | +| Book | Video | Code | X | |
| 14 | +| --- | --- | --- | --- | |
| 15 | +| [cppreference-integer_literal](https://en.cppreference.com/w/cpp/language/integer_literal) / [markdown](https://github.com/mcpp-community/d2mcpp/blob/main/book/en/src/cpp14/06-digit-separators.md) | [Video Explanation]() | [Exercise Code](https://github.com/mcpp-community/d2mcpp/blob/main/dslings/en/cpp14/06-digit-separators-0.cpp) | | |
| 16 | + |
| 17 | + |
| 18 | +**Why introduced?** |
| 19 | + |
| 20 | +`1000000000` is hard to parse, `0xFFFF0000` requires counting Fs. `1'000'000'000` and `0xFFFF'0000` make the numeric structure clear at a glance |
| 21 | + |
| 22 | +## I. Basic Usage and Scenarios |
| 23 | + |
| 24 | +```cpp |
| 25 | +int million = 1'000'000; // decimal |
| 26 | +int hex_val = 0xFF'FF'00'00; // hexadecimal |
| 27 | +int bin_val = 0b1010'1100'1111; // binary + separator |
| 28 | + |
| 29 | +static_assert(million == 1000000, ""); |
| 30 | +``` |
| 31 | + |
| 32 | +## II. Real-World Case — Digit Separators in the STL |
| 33 | + |
| 34 | +> The MSVC STL uses binary literals with digit separators in UTF-8 decoding. The example below cites the vendored [MSVC STL](https://github.com/mcpp-community/d2mcpp/tree/main/msvc-stl) (source: [`msvc-stl/stl/inc/format`](https://github.com/mcpp-community/d2mcpp/blob/main/msvc-stl/stl/inc/format#L281-L282)) |
| 35 | +
|
| 36 | +```cpp |
| 37 | +// MSVC STL · msvc-stl/stl/inc/format (abridged) |
| 38 | +// UTF-8 decoding — extract the lower 6 bits of each continuation byte |
| 39 | +_Val = (_Val << 6) | (static_cast<unsigned char>(_First[_Idx]) & 0b11'1111u); |
| 40 | +``` |
| 41 | + |
| 42 | +`0b11'1111u` is 63, and the `'` separator makes the binary mask's semantics directly visible in the literal — no mental conversion from `0x3F` or `63` |
| 43 | + |
| 44 | +## III. Notes |
| 45 | + |
| 46 | +- Cannot appear at the start or end: `'0` / `1'` are invalid |
| 47 | +- Cannot be consecutive: `1''0` is invalid |
| 48 | +- Does not affect the value, only improves readability |
| 49 | + |
| 50 | +## IV. Exercise Code |
| 51 | + |
| 52 | +### Exercise Topics |
| 53 | + |
| 54 | +- 0 - [Digit Separators — Large Numbers and Radix Combinations](https://github.com/mcpp-community/d2mcpp/blob/main/dslings/en/cpp14/06-digit-separators-0.cpp) |
| 55 | + |
| 56 | +### Auto-Checker Command |
| 57 | + |
| 58 | +``` |
| 59 | +d2x checker digit-separators |
| 60 | +``` |
| 61 | + |
| 62 | +## V. Other |
| 63 | + |
| 64 | +- [Discussion Forum](https://forum.d2learn.org/category/20) |
| 65 | +- [d2mcpp Tutorial Repository](https://github.com/mcpp-community/d2mcpp) |
| 66 | +- [Tutorial Video List](https://space.bilibili.com/65858958/lists/5208246) |
| 67 | +- [Tutorial Support Tool - xlings](https://github.com/openxlings/xlings) |
0 commit comments