Improving application design with std::variant and templates

Robert Schimkowitsch

⏱ 90 mins
beginner
intermediate
14:00-15:30, Friday, 19th June 2026

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 start with a simple kind of lambda-based compile-time polymorphism. Using 'if constexpr' and C++20 concepts, we increase our options and flexibility. By exploring and visualizing both compile-time and a run-time aspects of our code, we understand some issues that may seem baffling at first, and then learn how to resolve them. All through the talk, we consider the impact on application design, and discuss possible tradeoffs.

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.


🏷 variant
🏷 templates
🏷 concepts
🏷 techniques
🏷 constexpr
🏷 tradeoffs

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.