liveBook · Manning

Variant type

As explained in Object-Oriented Software Design in C++

A discriminated type whose value is one of a specified set of alternative datatypes.

A variant type stores one value selected from a specified set of alternative datatypes. In a book catalogue, AttributeValue can hold an int, string, or one of several enumeration types, giving every attribute-map entry a common value type. section 2.3.4

The design uses the variant to keep attribute changes localized:

  • Type indexes let the Attributes constructor verify that each key receives the correct datatype. section 2.3.4
  • Matching compares complete key–value pairs while allowing missing target attributes to act as don't-care fields. section 2.3.4

Book attributes

An AttributeMap can contain {KIND: fiction, TITLE: 'Carrie', YEAR: 1974, GENRE: horror}; the same map supports strings, integers, and enumeration values without separate map types.

How Object-Oriented Software Design in C++ covers it

How deep it goes
Defines the alternatives, stores them in a map, validates key–value types, and retrieves values during matching and output.
Examples
Demonstrates book kinds, titles, authors, years, genres, regions, and subjects as mixed typed attribute values.
What it assumes
Assumes familiarity with C++ maps, enumerations, constructors, and assertions.
What you can do after
Apply it via C++ <variant> to build one map that safely stores several specified value types.
How it connects
Connects variant types with enumerations, maps, defensive programming, attribute matching, and catalogue search.

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.