Skip to content

Commit 5a36c27

Browse files
committed
Final updates
1 parent a769606 commit 5a36c27

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

animation/variant/src/scenes/example.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ int main() {
258258
#include <variant>
259259
260260
int main() {
261-
const auto Print = [](auto value) { std::cout << value << '\\n'; };
261+
const auto Print = [](const auto& value) { std::cout << value << '\\n'; };
262262
263263
std::variant<int, std::string> value{};
264264
std::visit(Print, value);

lectures/variant.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
--
44

55
<p align="center">
6-
<a href="https://youtu.be/dummy_link"><img src="https://img.youtube.com/vi/dummy_link/maxresdefault.jpg" alt="Video Thumbnail" align="right" width=50% style="margin: 0.5rem"></a>
6+
<a href="https://youtu.be/mNeu4S0x3gA"><img src="https://img.youtube.com/vi/mNeu4S0x3gA/maxresdefault.jpg" alt="Video Thumbnail" align="right" width=50% style="margin: 0.5rem"></a>
77
</p>
88

99
- [`std::variant` in Modern C++](#stdvariant-in-modern-c)
@@ -251,7 +251,7 @@ struct NonDefaultConstructibleType {
251251

252252
// ❌ Can't compile if first type is not default constructible.
253253
// 💡 Other types don't play a role here.
254-
int main() { std::variant<NonDefaultConstructibleType, int, double> v; }
254+
int main() { std::variant<NonDefaultConstructibleType, int, double> v{}; }
255255
```
256256
257257
This code won't compile, throwing an error that tells us that a constructor of the variant is ignored because it does not satisfy the requirement `std::is_default_constructible<NonDefaultConstructibleType>`.
@@ -261,7 +261,7 @@ This code won't compile, throwing an error that tells us that a constructor of t
261261
262262
```css
263263
<source>:7:69: error: no matching constructor for initialization of 'std::variant<NonDefaultConstructibleType, int, double>'
264-
7 | int main() { std::variant<NonDefaultConstructibleType, int, double> v; }
264+
7 | int main() { std::variant<NonDefaultConstructibleType, int, double> v{}; }
265265
| ^
266266
/cefs/d2/d2e6ebb9fe16525f6e7eb0c3_consolidated/compilers_c++_clang_17.0.1/bin/../include/c++/v1/variant:1326:13: note: candidate template ignored: requirement '__dependent_type<std::is_default_constructible<NonDefaultConstructibleType>, true>::value' was not satisfied [with _Dummy = true]
267267
1326 | constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
@@ -315,12 +315,12 @@ To mitigate such situations there is a type `std::monostate` in the standard lib
315315
```cpp
316316
#include <variant>
317317

318-
struct NonDefaultConstructibleType{
318+
struct NonDefaultConstructibleType {
319319
NonDefaultConstructibleType(int) {}
320320
};
321321

322322
int main() {
323-
std::variant<std::monostate, NonDefaultConstructibleType, int, double> value{};
323+
std::variant<std::monostate, NonDefaultConstructibleType, int, double> v{};
324324
// value holds an instance of std::monostate for now.
325325
}
326326
```
@@ -409,7 +409,7 @@ And while we used an explicit function object for illustration here, we could as
409409
#include <variant>
410410
411411
int main() {
412-
const auto Print = [](auto value) { std::cout << value << std::endl; };
412+
const auto Print = [](const auto& value) { std::cout << value << std::endl; };
413413
414414
std::variant<int, std::string> value{};
415415
std::visit(Print, value);

readme.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,29 @@ Headers with classes
762762
----------------------------------------------------------
763763
</details>
764764

765+
<details>
766+
<summary>Dynamic polymorphism with <code>std::variant</code></summary>
767+
768+
----------------------------------------------------------
769+
[![Video thumbnail](https://img.youtube.com/vi/mNeu4S0x3gA/maxresdefault.jpg)](https://youtu.be/mNeu4S0x3gA)
770+
771+
- [`std::variant` in Modern C++](lectures/variant.md#stdvariant-in-modern-c)
772+
- [Templates (and concepts) allow static polymorphism](lectures/variant.md#templates-and-concepts-allow-static-polymorphism)
773+
- [Until now dynamic polymorphism required reference semantics](lectures/variant.md#until-now-dynamic-polymorphism-required-reference-semantics)
774+
- [Use `std::variant` for dynamic polymorphism with value semantics](lectures/variant.md#use-stdvariant-for-dynamic-polymorphism-with-value-semantics)
775+
- [Basics of `std::variant`](lectures/variant.md#basics-of-stdvariant)
776+
- [Memory used by `std::variant`](lectures/variant.md#memory-used-by-stdvariant)
777+
- [Use `std::monostate` to allow for "empty" variants](lectures/variant.md#use-stdmonostate-to-allow-for-empty-variants)
778+
- [Storing values in a `std::variant`](lectures/variant.md#storing-values-in-a-stdvariant)
779+
- [Using `std::variant` with `std::visit`](lectures/variant.md#using-stdvariant-with-stdvisit)
780+
- [How `std::visit` selects the correct function](lectures/variant.md#how-stdvisit-selects-the-correct-function)
781+
- [Visitor must cover all variant types](lectures/variant.md#visitor-must-cover-all-variant-types)
782+
- [Back to the original example](lectures/variant.md#back-to-the-original-example)
783+
- [**Summary**](lectures/variant.md#summary)
784+
785+
----------------------------------------------------------
786+
</details>
787+
765788
## PS
766789

767790
### Most of the code snippets are validated automatically

0 commit comments

Comments
 (0)