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
Add 'since C++NN' markers to all Chapter 3 features, and give
std::function a motivation lead explaining the problem it solves
(uniformly storing/passing the many kinds of callable). The lambda,
move-semantics, and value-category sections already motivate well.
Copy file name to clipboardExpand all lines: book/en-us/03-runtime.md
+18Lines changed: 18 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,6 +16,8 @@ So anonymous functions are almost standard in modern programming languages.
16
16
17
17
### Basics
18
18
19
+
*(since C++11)*
20
+
19
21
The basic syntax of a Lambda expression is as follows:
20
22
21
23
```
@@ -124,6 +126,8 @@ initialize it in the expression.
124
126
125
127
### Generic Lambda
126
128
129
+
*(since C++14)*
130
+
127
131
In the previous section, we mentioned that the `auto` keyword cannot be used
128
132
in the parameter list because it would conflict with the functionality of the template.
129
133
But lambda expressions are not regular functions, without further specification on the typed parameter list, lambda expressions cannot utilize templates. Fortunately, this trouble
@@ -149,6 +153,10 @@ This part of the content is also very important, so put it here for the introduc
149
153
150
154
### `std::function`
151
155
156
+
*(since C++11)*
157
+
158
+
In C++, "things that can be called" come in many shapes — ordinary functions, function pointers, lambda expressions, and any object that overloads `operator()` — and they all have different types, which makes them hard to store and pass around uniformly. `std::function` exists precisely to solve this: it is a type-safe "container for callables" that can uniformly store, copy, and invoke any callable target, letting us handle "functions" as ordinary objects.
159
+
152
160
The essence of a Lambda expression is an object of a class type (called a closure type)
153
161
that is similar to a function object type (called a closure object).
154
162
When the capture list of a Lambda expression is empty, the closure object
@@ -206,6 +214,8 @@ int main() {
206
214
207
215
### `std::bind` and `std::placeholder`
208
216
217
+
*(since C++11)*
218
+
209
219
And `std::bind` is used to bind the parameters of the function call.
210
220
It solves the requirement that we may not always be able to get all the parameters
211
221
of a function at one time. Through this function, we can Part of the call parameters
@@ -331,6 +341,8 @@ This is the move semantics we will mention later.
331
341
332
342
### rvalue reference and lvalue reference
333
343
344
+
*(since C++11)*
345
+
334
346
To get a xvalue, you need to use the declaration of the rvalue reference: `T &&`,
335
347
where `T` is the type.
336
348
The statement of the rvalue reference extends the lifecycle of this temporary value,
@@ -413,6 +425,8 @@ The reason is simple because Fortran needs it.
413
425
414
426
### Move semantics
415
427
428
+
*(since C++11)*
429
+
416
430
Traditional C++ has designed the concept of copy/copy for class objects
417
431
through copy constructors and assignment operators,
418
432
but to implement the movement of resources,
@@ -499,6 +513,8 @@ int main() {
499
513
500
514
### Perfect forwarding
501
515
516
+
*(since C++11)*
517
+
502
518
As we mentioned earlier, the rvalue reference of a declaration is actually an lvalue.
503
519
This creates problems for us to parameterize (pass):
504
520
@@ -639,6 +655,8 @@ Because when `auto` is pushed to a different lvalue and rvalue reference, the co
639
655
640
656
### Guaranteed copy elision
641
657
658
+
*(since C++17)*
659
+
642
660
Before C++17, when an object was initialized from a prvalue of the same type, the compiler *was permitted* (but not required) to omit the copy/move construction — this is known as copy elision. Because it was merely allowed, the object's type still had to have an accessible copy or move constructor, even though it would not actually be called.
643
661
644
662
C++17 makes copy elision **guaranteed** in this case: when an object is initialized from a prvalue of the same type, there is no temporary object at all — the object is constructed directly in its target location. As a result, returning a prvalue by value is well-formed even for a type that is neither copyable nor movable:
0 commit comments