Skip to content

Commit e074cc0

Browse files
fix: ci build crash
1 parent 7a770b7 commit e074cc0

2 files changed

Lines changed: 8 additions & 8 deletions

File tree

documents/en/vol3-standard-library/11-initializer-lists.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ cpp_standard:
55
- 14
66
- 17
77
description: 'Deep dive into `std::initializer_list`: the compiler-generated read-only
8-
view for `{...}`, shallow copies and `const` elements, the "move trap" where elements
8+
view for {...}, shallow copies and `const` elements, the "move trap" where elements
99
cannot be moved into containers, overload resolution priority for brace initialization,
1010
and its relationship with container constructors.'
1111
difficulty: intermediate
@@ -30,7 +30,7 @@ translation:
3030
---
3131
# std::initializer_list: The Lightweight Sequence Behind Braces
3232

33-
## What is initializer_list: A Read-Only View Generated by the Compiler for {…}
33+
## What is initializer_list: A Read-Only View Generated by the Compiler for `{...}`
3434

3535
`std::initializer_list` is the standard library type introduced in C++11 to support "braced list initialization". When you write `std::vector<int> v = {1, 2, 3}` or `func({1.0, 2.0})`, the compiler constructs an `initializer_list` behind the scenes, representing the sequence `{1, 2, 3}`. It is an extremely lightweight object—essentially just a pointer and a length, similar to `std::string_view`, belonging to the category of "views that do not own data."
3636

@@ -50,7 +50,7 @@ void func(std::initializer_list<int> list); // Passing by value is cheap (shallo
5050

5151
But remember the "elements are const" rule: the elements inside an `initializer_list` are `const T&`. You cannot get non-const access. This seems harmless, but it digs a huge pit when combined with move semantics—we'll cover that in the next section.
5252

53-
## The Move Trap: Elements in {…} Can Only Be Copied into Containers
53+
## The Move Trap: Elements in `{...}` Can Only Be Copied into Containers
5454

5555
This is the classic `initializer_list` pitfall. You want to shove a few objects into a `vector`, so you write `std::vector<BigObj> v{obj1, obj2, obj3}` expecting modern C++ to efficiently move them—result: they are **copied** in.
5656

@@ -84,7 +84,7 @@ Let's compare these three scenarios. The first one `{c1, c2, c3}` (lvalues): 6 c
8484
8585
So remember this performance pitfall: **when putting several objects into a container, `{...}` still copies into the vector, only `push_back`/`emplace_back` gives zero copies**. When `T` is a heavy type (large `string`, large `vector`), this difference is real copy overhead.
8686
87-
## Brace Priority: Why {…} Always Prefers Matching initializer_list
87+
## Brace Priority: Why `{...}` Always Prefers Matching initializer_list
8888
8989
`initializer_list` has an "overload preference": as long as a class has a constructor taking `std::initializer_list`, brace initialization will prioritize it, even if another constructor seems a better fit. The most classic crash site is `std::vector`:
9090

documents/vol3-standard-library/11-initializer-lists.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cpp_standard:
44
- 11
55
- 14
66
- 17
7-
description: 讲透 std::initializer_list:编译器为 {} 生成的只读视图、浅拷贝与 const 元素、元素无法移动进容器的「移动陷阱」、花括号初始化的重载优先级,以及与容器构造的关系
7+
description: 讲透 std::initializer_list:编译器为 {...} 生成的只读视图、浅拷贝与 const 元素、元素无法移动进容器的「移动陷阱」、花括号初始化的重载优先级,以及与容器构造的关系
88
difficulty: intermediate
99
order: 11
1010
platform: host
@@ -21,7 +21,7 @@ title: std::initializer_list:花括号背后的轻量序列
2121
---
2222
# std::initializer_list:花括号背后的轻量序列
2323

24-
## initializer_list 是什么:编译器为 {…} 生成的只读视图
24+
## initializer_list 是什么:编译器为 `{...}` 生成的只读视图
2525

2626
`std::initializer_list` 是 C++11 给「花括号列表初始化」配的标准库类型。你写 `vector<int>{1, 2, 3}``f({1, 2, 3})` 时,编译器会在背后构造一个 `std::initializer_list<int>`,代表 `{1, 2, 3}` 这段序列。它本身是个极轻量的对象——大致就是一个指针加一个长度,和 `span` 一样属于「不拥有数据的视图」。
2727

@@ -45,7 +45,7 @@ f({1, 2, 3, 4, 5}); // 不拷贝 5 个 int,只传一个视图
4545

4646
但「元素是 const」这一点要记住:initializer_list 里的元素是 `const T`,你拿不到非 const 访问。这看起来无害,却在和移动语义结合时挖了个大坑——下一节专门说。
4747

48-
## 移动陷阱:{…} 里的元素,进容器时只能拷贝
48+
## 移动陷阱:`{...}` 里的元素,进容器时只能拷贝
4949

5050
这是 initializer_list 最经典的坑。你想把几个对象塞进 vector,顺手写了 `vector<T>{a, b, c}`,以为现代 C++ 会高效地移动它们——结果它们是**拷贝**进去的。
5151

@@ -119,7 +119,7 @@ push_back(move) : copies=0 moves=3
119119

120120
所以记住这个性能坑:**把若干对象塞进容器,`vector{move(a), ...}` 仍会拷贝进 vector,只有 `push_back(move)` 才零拷贝**。当 T 是重型类型(大 string、大 vector),这个差距是实打实的拷贝开销。
121121

122-
## 花括号优先:为什么 {…} 总爱匹配 initializer_list 构造
122+
## 花括号优先:为什么 `{...}` 总爱匹配 initializer_list 构造
123123

124124
initializer_list 还有个「重载偏好」:只要一个类的构造函数有 `initializer_list` 版本,花括号初始化就会优先选它,哪怕别的构造函数看起来更「合身」。最经典的翻车现场是 `vector<int>`
125125

0 commit comments

Comments
 (0)