February 2021
Intermediate to advanced
1312 pages
37h 6m
English
This section examines the vocabulary types variant and any.
std::variant, defined in <variant>, can hold a single value of one of a given set of types. When you define a variant, you have to specify the types it can potentially contain. For example, the following code defines a variant that can contain an integer, a string, or a floating-point value, one at a time:
variant<int, string, float> v;
The template type arguments for a variant must be unique; for example, variant<int,int> is invalid. A default-constructed variant contains a default-constructed value of its first type, int in the case of the variant v. If you want to be able to default construct a variant, you have to make sure that ...
Read now
Unlock full access