| Start |
|---|
| Create pseudo-method pointers This is an advanced example. You can use variant for light weight type erasure. this creates a type that overloads Now if we have 2 types each with a note that they are unrelated types. We can: and it will dispatch the call directly to If we created a new type C: then: will fail to compile, because there is no Extending the above would permit free function live example (opens new window) currently using |
| Basic std::variant use This creates a variant (a tagged union) that can store either an We can store one of either type in it: And we can access the contents via by passing in a polymorphic lambda or similar function object. If we are certain we know what type it is, we can get it: but this will throw if we get it wrong. returns Variants guarantee no dynamic memory allocation (other than which is allocated by their contained types). Only one of the types in a variant is stored there, and in rare cases (involving exceptions while assigning and no safe way to back out) the variant can become empty. Variants let you store multiple value types in one variable safely and efficiently. They are basically smart, type-safe |
Constructing a std::variant This does not cover allocators. RemarksVariant is a replacement for raw It is almost never empty: only in corner cases where replacing its content throws and it cannot back out safely does it end up being in an empty state. It behaves somewhat like a Using |
Read Article
https://develop.horje.com/learn/1434/reference