@@ -25,11 +25,11 @@ Advanced Generic lambdas, utilizing mutable state, mapping
25
25
_ Why is this important?_
26
26
_ Why do we want to learn/teach this topic?_
27
27
28
- * function objects in relation to the standard library...
28
+ * Function objects in relation to the standard library, e.g. for passing functions to the standard algorithms or auxiliary functions
29
29
* Allows the developer to define and pass functionality as values/data/information
30
30
* Allows developer to operate in a functional-programming mindset
31
- * Improves code readdability by localizing the code
32
- * Effective means to help refactoring code from long functions to re-usable functions
31
+ * Improves code readability by localizing the code
32
+ * Making code more compact by writing functions in-place
33
33
* Library designs taking callables allows the library to be customizable for the consumer, while lambdas make this usage approachable
34
34
35
35
### Topic introduction
@@ -44,7 +44,7 @@ Lambdas were added in C++11 and have grown in power and functionality ever since
44
44
45
45
A student:
46
46
_ TODO: Add cross-reference to these
47
- Explain capture-by- value and capture-by- reference
47
+ Explain function argument passing by value or by reference
48
48
49
49
#### Student outcomes
50
50
@@ -55,10 +55,11 @@ _Max 5 items._
55
55
A student should be able to:
56
56
57
57
1 . Use a lambda taking a concrete type as a parameter and return a value
58
- 2 . transform a function definition into a lambda and use it
59
- 3 . enumerate trade-offs of code-locality using lambdas vs code reuse with free-functions
60
- 4 . assign a lambda to a variable for multiple calls
61
- 5 . named lambdas to increase code readability
58
+ 2 . Transform a function definition into a lambda and use it
59
+ 3 . Enumerate trade-offs of code-locality using lambdas vs code reuse with free-functions
60
+ 4 . Assign a lambda to a variable for multiple calls
61
+ 5 . Named lambdas to increase code readability
62
+ 6 . Explain capture-by-value and capture-by-reference
62
63
63
64
64
65
@@ -83,9 +84,10 @@ A student should be able to:
83
84
84
85
1 . Utilize ` std::function ` as a means to hold and transfer lambdas
85
86
2 . Define a function-template taking a lambda as a template parameter
86
- 3 . specify, per parameter, whether to capture by reference or value
87
- 4 . specify the default capture type for a lambda
88
- 5 . use a lambda in a class, capturing and utilizing class data via ` this `
87
+ 3 . Specify, per parameter, whether to capture by reference or value
88
+ 4 . Specify the default capture type for a lambda
89
+ 5 . Specify the return type for a lamda function, if not detected by the compiler
90
+ 5 . Use a lambda in a class, capturing and utilizing class data via ` this `
89
91
90
92
#### Caveats
91
93
@@ -111,38 +113,40 @@ guidance where one can continue to investigate this topic in more depth._
111
113
A student should be able to:
112
114
113
115
1 . Use a lambda to introduce a new identifier for use within the body of the lambda
114
- 2 . explain the relationships between generic lambdas and templates
115
- 3 . construct an object with choice between 2 identifiers , using immediately-dispathced lambda
116
- 4 . utilize an immediately dispatched lambda to encode multiple statements where the language requires an expression
117
- 5 . use the ` mutable ` keyword to allow changing a captured-by-value or lambda-local value within the lambda body
118
- 6 . explicitly specify the return type for a lambda
119
- 7 . explain under what conditions an explicit return type is necessary
120
-
116
+ 2 . Explain the relationships between generic lambdas and templates
117
+ 3 . Construct an object with choice between 2 constructors , using immediately-dispatched lambda (see first listing)
118
+ 4 . Utilize an immediately dispatched lambda to encode multiple statements where the language requires an expression
119
+ 5 . Use the ` mutable ` keyword to allow changing a captured-by-value or lambda-local value within the lambda body
120
+ 6 . Explicitly specify the return type for a lambda
121
+ 7 . Explain under what conditions an explicit return type is necessary
122
+
121
123
#### Caveats
122
124
123
125
#### Points to cover
124
126
125
- Construct an object with choice between 2 identifiers :
127
+ Construct an object with choice between 2 constructors :
126
128
```
127
129
class X;
128
130
bool b;
129
131
X x = [c = b](){ if (c) return X(5, 10) else X("a", "b"); }();
130
132
```
131
133
132
- multiple statements in a single expression
134
+ Todo add caption
133
135
```
134
136
assert([](){
135
137
std::vector v{1, 2, 3, 4, 5};
136
138
for (auto x: v) std::cout << v << std::endl;
137
139
}());
138
140
```
139
141
140
- Capture can introduce new names
142
+
143
+ Capture can introduce new names by initializing them as ` capture-default `
141
144
```
142
145
char const* const s = "5";
143
146
[x=atoi(s)](){}
144
147
```
145
148
149
+ ` Capture-default ` variables can be altered by the lambda body if the lamda is marked as ` mutable `
146
150
```
147
151
std::ranges::for_each(
148
152
std::vector{1,2,3,4,5},
0 commit comments