Improving application design with std::variant and templates
Robert Schimkowitsch
A std::variant can store a value of one type at a time, out of a user-defined set of types. Those types can be completely unrelated to one another, but we can design them to have similarities. Templates allow us to process unrelated types, if there is a common way to interact with them. Bring those two observations together, and wonderful things become possible.
In this talk, we explore combining template techniques with std::variant. The result? Extensible code with less boilerplate.
We cover the basics of the subject area, then introduce an example inspired by my own work experience. As we dive into some key code snippets essential to the overall design, we identify problems, then figure out how to solve them. On this journey, we learn about breaking dependencies, ergonomic use of distinct data types and replacing duplicate code with a single implementation. We look at template instantiations to visualize and understand both the compile-time and run-time aspects of our code. Finally, we evaluate the code design impact of our changes, what benefits they gave us, and what alternative options we could have used instead.
After this talk, you will have a firm grasp on techniques that make std::variant even more useful in practice. This will give you more options for your next application subsystem design. You do not need prior knowledge of either std::variant or writing template code.
Robert Schimkowitsch
Robert has been a professional application developer for 25 years, 15 of them using C++ and Qt. Working on long-lived software, he knows how to create code that is safe, reliable and easy to maintain (by first writing lots of code that was not). Always learning new things, Robert often feels the urge to throw away everything older than 3 years, but is (usually) smart enough not to do so. He enjoys working with others and likes to emphasize the social aspects of software development, as with the C++ User Group Vienna (Austria) that he co-founded.