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: code/ExercisesCheatSheet.md
+20
Original file line number
Diff line number
Diff line change
@@ -140,6 +140,19 @@ Here we have four code snippets that will benefit from using smart pointers.
140
140
- `problem3` is an example of shared ownership where `std::shared_pointer` should be used.
141
141
- `problem4` demonstrates the usage of `shared_ptr` as class members. It has a second part where a `weak_ptr` can be used, but can be skipped if not enough time.
142
142
143
+
### std::optional (directory: `optional`)
144
+
Use std::optional to signify disallowed values in a computation.
145
+
1. Use std::optional as return value of the mysqrt function. Use `nullopt_t` for negative arguments. Note that `return {}` will create a `nullopt_t` automatically.
146
+
2. Given that the return type changes, modify the square function accordingly to take into account cases where a `nullopt_t` is given as input. Note that `std::optional` can be directly used in an if statement as if it would be a boolean to check whether is value is present
147
+
148
+
### std::variant (directory: `variant`)
149
+
Use the variant as an alternative to inheritance. The goal is to understand
150
+
1. That the base class is unnecessary when variant is used
151
+
2. That no dynamic allocations and polymorphism are necessary because the variant can directly be pushed into the vector
152
+
3. That there's multiple ways to "visit" the vector.
153
+
- Either with explicit checking of the type (solution 1)
154
+
- Or automatic visitation with std::visit (solution 2). Note that generic lambdas make the std::visit solution extremely short.
155
+
143
156
144
157
Expert Exercises
145
158
----------------
@@ -170,6 +183,13 @@ Solutions:
170
183
To intersperse the ", " between elements, you need more logic so a fold over << won't work. Use a fold over comma.
171
184
Finally, to omit the last ", ", just use a conditional expression comparing the expanded index pack against the index pack size
172
185
186
+
187
+
### Spaceship operator <=>
188
+
The goal is to write a custom <=>, and to understand that when ordering according to the norm of a complex, a `partial_ordering` is required.
189
+
Many numbers can have the same norm, but a different phase.
190
+
Given that an implementation for <=> is provided, an implementation for == is required as well. It's OK to default it. It is not possible to
0 commit comments