You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documents/en/vol3-standard-library/11-initializer-lists.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ cpp_standard:
5
5
- 14
6
6
- 17
7
7
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
9
9
cannot be moved into containers, overload resolution priority for brace initialization,
10
10
and its relationship with container constructors.'
11
11
difficulty: intermediate
@@ -30,7 +30,7 @@ translation:
30
30
---
31
31
# std::initializer_list: The Lightweight Sequence Behind Braces
32
32
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 `{...}`
34
34
35
35
`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."
36
36
@@ -50,7 +50,7 @@ void func(std::initializer_list<int> list); // Passing by value is cheap (shallo
50
50
51
51
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.
52
52
53
-
## The Move Trap: Elements in {…} Can Only Be Copied into Containers
53
+
## The Move Trap: Elements in `{...}` Can Only Be Copied into Containers
54
54
55
55
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.
56
56
@@ -84,7 +84,7 @@ Let's compare these three scenarios. The first one `{c1, c2, c3}` (lvalues): 6 c
84
84
85
85
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.
`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`:
0 commit comments