Roi Barkan: Variations on Variant


Details
std::variant was added to C++ in 2017 as a type-safe alternative to C's union. To start us off, I will shortly introduce the rationale of std::variant and the typical ways in which it can be used. I will also try to give insights into the way std::variant is typically implemented, highlighting the memory layout and its run-time performance implications.
The bulk of the talk will then focus on the key differences between std::variant and union, and discuss a common use-case where unions are typically used which std::variant does not support - serialization of streams of a trivially copyable object to files or to network. Then, I'll suggest 3 approaches to tackle this shortcoming: Intrusive-Variant, Variant-of-Base, and Variant-of-BasePtr. After presenting and describing the 3 approaches above, I will dive deeper into the counterintuitive uses of std::variant, such as a simple and safe mechanism for de-virtualization.
After attending this talk, participants should be better equipped to use this relatively new concept to modernize existing low-level C style code and will have a better understanding of how variants work under the hood.
Where you will find us: https://meet.osna.social/osnacpp
Please disable your video feed when you join.

Roi Barkan: Variations on Variant