std::variant - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::variant

From cppreference.com
< cpp‎ | utility

Defined in header `<variant>` |  |
template <class... Types> class variant; |  | (since C++17)

The class template `std::variant` represents a type-safe union. An instance of `std::variant` at any given time either holds a value of one of its alternative types, or it holds no value (this state is hard to achieve, see `valueless_by_exception`).

As with unions, if a variant holds a value of some object type `T`, the object representation of `T` is allocated directly within the object representation of the variant itself. Variant is not allowed to allocate additional (dynamic) memory.

A variant is not permitted to hold references, arrays, or the type `void`. Empty variants are also ill-formed (std::variant<std::monostate> can be used instead).

A variant is permitted to hold the same type more than once, and to hold differently cv-qualified versions of the same type.

As with unions, the default-initialized variant holds a value of its first alternative, unless that alternative is not default-constructible (in which case default constructor won't compile: the helper class `std::monostate` can be used to make such variants default-constructible)

### [edit] Template parameters

Types | - | the types that may be stored in this variant. All types must be (possibly cv-qualified) non-array object types.

### [edit] Member functions

(constructor) | constructs the variant object (public member function) [edit]
(destructor) | destroys the variant, along with its contained value (public member function) [edit]
operator= | assigns a variant (public member function) [edit]
##### Observers
index | returns the zero-based index of the alternative held by the variant (public member function) [edit]
valueless_by_exception | checks if the variant is in the invalid state (public member function) [edit]
##### Modifiers
emplace | constructs a value in the variant, in place (public member function) [edit]
swap | swaps with another variant (public member function) [edit]

### [edit] Non-member functions

visit (C++17) | calls the provided functor with the arguments held by one or more variants (function template) [edit]
holds_alternative (C++17) | checks if a given type appears exactly once in a variant (function template) [edit]
std::get(std::variant) (C++17) | reads the value of the variant given the index or the type (if the type is unique), throws on error (function template) [edit]
get_if (C++17) | obtains a pointer to the value of a pointed-to variant given the index or the type (if unique), returns null on error (function template) [edit]
operator==operator!=operator<operator<=operator>operator>= (C++17) | compares `variant` objects as their contained values (function template) [edit]
std::swap(std::variant) (C++17) | specializes the std::swap algorithm (function) [edit]

### [edit] Helper classes

monostate (C++17) | placeholder type for use as the first alternative in a variant of non-default-contructible types (class) [edit]
bad_variant_access (C++17) | exception thrown on invalid accesses to the value of a variant (class) [edit]
variant_sizevariant_size_v (C++17) | obtains the size of the variant's list of alternatives at compile time (class template) (variable template) [edit]
variant_alternativevariant_alternative_t (C++17) | obtains the type of the alternative specified by its index, at compile time (class template) (alias template) [edit]
std::hash<std::variant> (C++17) | specializes the std::hash algorithm (class template specialization) [edit]
std::uses_allocator<std::variant> (C++17) | specializes the std::uses_allocator type trait (class template specialization) [edit]

### [edit] Helper objects

variant_npos (C++17) | index of the variant in the invalid state (constant) [edit]

### [edit] Example

Run this code

    #include <variant>
    #include <string>

    int main()
    {
        std::variant<int, float> v, w;
        v = 12; // v contains int
        int i = std::get<int>(v);
        w = std::get<int>(v);
        w = std::get<0>(v); // same effect as the previous line
        w = v; // same effect as the previous line

    //  std::get<double>(v); // error: no double in [int, float]
    //  std::get<3>(v);      // error: valid index values are 0 and 1

        try {
          std::get<float>(w); // w contains int, not float: will throw
        }
        catch (std::bad_variant_access&) {}

        std::variant<std::string> v("abc"); // converting constructors work when unambiguous
        v = "def"; // converting assignment also works when unambiguous
    }

### [edit] See also

in_place in_place_type in_place_index in_place_t in_place_type_t in_place_index_t (C++17) | in-place construction tag (class template) [edit]
optional (since C++17) | a wrapper that may or may not hold an object (class template) [edit]
any (since C++17) | Objects that hold instances of any `CopyConstructible` type. (class) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/utility/variant&oldid=89738"