Skip to Content
C++ Templates: The Complete Guide, 2nd Edition
book

C++ Templates: The Complete Guide, 2nd Edition

by David Vandevoorde, Nicolai M. Josuttis, Douglas Gregor
September 2017
Intermediate to advanced
822 pages
26h 51m
English
Addison-Wesley Professional
Content preview from C++ Templates: The Complete Guide, 2nd Edition

Chapter 26

Discriminated Unions

The tuples developed in the previous chapter aggregate values of some list of types into a single value, giving them roughly the same functionality as a simple struct. Given this analogy, it is natural to wonder what the corresponding type would be for a union: It would contain a single value, but that value would have a type selected from some set of possible types. For example, a database field might contain an integer, floating-point value, string, or binary blob, but it can only contain a value of one of those types at any given time.

In this chapter, we develop a class template Variant that dynamically stores a value of one of a given set of possible value types, similar to the C++17 standard library’s std::variant<> ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C++ Software Design

C++ Software Design

Klaus Iglberger

Publisher Resources

ISBN: 9780134778808