std::variant::variant - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::variant::variant

From cppreference.com
< cpp‎ | utility‎ | variant

constexpr variant() noexcept(/* see below */); | (1) | (since C++17)
variant(const variant& other); | (2) | (since C++17)
variant(variant&& other) noexcept(/* see below */); | (3) | (since C++17)
template< class T > constexpr variant(T&& t) noexcept(/* see below */); | (4) | (since C++17)
template< class T, class... Args > constexpr explicit variant(std::in_place_type_t<T>, Args&&... args); | (5) | (since C++17)
template< class T, class U, class... Args > constexpr explicit variant(std::in_place_type_t<T>, std::initializer_list<U> il, Args&&... args); | (6) | (since C++17)
template< std::size_t I, class... Args > constexpr explicit variant(std::in_place_index_t<I>, Args&&... args); | (7) | (since C++17)
template <size_t I, class U, class... Args> constexpr explicit variant(std::in_place_index_t<I>, std::initializer_list<U> il, Args&&... args); | (8) | (since C++17)
template <class Alloc> variant(std::allocator_arg_t, const Alloc& a); | (9) | (since C++17)
template <class Alloc> variant(std::allocator_arg_t, const Alloc& a, const variant& other); | (10) | (since C++17)
template <class Alloc> variant(std::allocator_arg_t, const Alloc& a, variant&& other); | (11) | (since C++17)
template <class Alloc, class T> variant(std::allocator_arg_t, const Alloc& a, T&& t); | (12) | (since C++17)
template <class Alloc, class T, class... Args> variant(std::allocator_arg_t, const Alloc& a, std::in_place_type_t<T>, Args&&... args); | (13) | (since C++17)
template <class Alloc, class T, class U, class... Args> variant(std::allocator_arg_t, const Alloc& a, std::in_place_type_t<T>, std::initializer_list<U> il, Args&&... args); | (14) | (since C++17)
template <class Alloc, size_t I, class... Args> variant(std::allocator_arg_t, const Alloc& a, std::in_place_index_t<I>, Args&&... args); | (15) | (since C++17)
template <class Alloc, size_t I, class U, class... Args> variant(std::allocator_arg_t, const Alloc& a, std::in_place_index_t<I>, std::initializer_list<U> il, Args&&... args); | (16) | (since C++17)

Constructs a new `variant` object.

1) Default constructor. Constructs a variant holding the value-initialized value of the first alternative (`index()` is zero). This constructor is `constexpr` if and only if the value initialization of the alternative type T_0 would satisfy the requirements for a constexpr function. This overload only participates in overload resolution if std::is_default_constructible_v<T_0> is true.
2) Copy constructor. If `other` is not valueless_by_exception, constructs a variant holding the same alternative as `other` and direct-initializes the contained value with std::get<other.index()>(other). Otherwise, initializes a valueless_by_exception variant. This overload only participates in overload resolution if std::is_copy_constructible_v<T_i> is true for all `T_i` in `Types...`.
3) Move constructor. If `other` is not valueless_by_exception, constructs a variant holding the same alternative as `other` and direct-initializes the contained value with std::get<other.index()>(std::move(other)). Otherwise, initializes a `valueless_by_exception` variant. This overload only participates in overload resolution if std::is_move_constructible_v<T_i> is true for all `T_i` in `Types...`
4) Converting constructor. Constructs a variant holding the alternative type `T_j` that would be selected by overload resolution for the expression F(std::forward<T>(t) if there was an overload of imaginary function F(T_i) for every T_i from Types... in scope at the same time. Direct-initializes the contained value as if by direct non-list-initialization from std::forward<T>(t). This overload only participates in overload resolution if std::is_same_v<std::decay_t<T>, variant> is `false`, std::decay_t<T> is neither a specialization of std::in_place_type_t nor a specialization of std::in_place_index_t, std::is_constructible_v<T_j, T> is `true`, and the expression F(std::forward<T>(t)) (with F being the above-mentioned set of imaginary functions) is well formed. This constructor is a constexpr constructor if T_j's selected constructor is a constexpr constructor.

    variant<string> v("abc"); // OK
    variant<string, string> w("abc"); // ill-formed, can't select the alternative to convert to
    variant<string, bool> x("abc"); // OK, but chooses bool

5) Constructs a variant with the specified alternative `T` and initializes the contained value with the arguments std::forward<Args>(args).... If T's selected constructor is a constexpr constructor, this constructor is also a constexpr constructor. This overload only participates in overload resolution if there is exactly one occurrence of T in `Types...` and std::is_constructible_v<T, Args...> is `true`.
6) Constructs a variant with the specified alternative `T` and initializes the contained value with the arguments il, std::forward<Args>(args)..... If T's selected constructor is a constexpr constructor, this constructor is also a constexpr constructor. This overload only participates in overload resolution if there is exactly one occurrence of T in `Types...` and std::is_constructible_v<T, initializer_list<U>&, Args...> is `true`.
7) Constructs a variant with the alternative T_i specified by the index `I` and initializes the contained value with the arguments std::forward<Args>(args).... If T_i's selected constructor is a constexpr constructor, this constructor is also a constexpr constructor. This overload only participates in overload resolution if I < sizeof...(Types) and std::is_constructible_v<T_i, Args...> is true.
8) Constructs a variant with the alternative T_i specified by the index `I` and initializes the contained value with the arguments il, std::forward<Args>(args).... If T_i's selected constructor is a constexpr constructor, this constructor is also a constexpr constructor. This overload only participates in overload resolution if I < sizeof...(Types) and std::is_constructible_v<T_i, std::initializer_list<U>&, Args...> is true.
9-16) Same as (1-8), except the contained value is constructed following the uses-allocator protocol

### [edit] Parameters

other | - | another `variant` object whose contained value to copy/move
t | - | value to initialize the contained value with
args... | - | arguments to initialize the contained value with
il | - | initializer list to initialize the contained value with
a | - | allocator to pass to the contained value
Type requirements
- `Alloc` must meet the requirements of `Allocator` in order to use overloads (9).

### [edit] Exceptions

1) May throw any exception thrown by the value initialization of the first alternative.
`noexcept` specification:
noexcept(std::is_nothrow_default_constructible_v<T_0>)
2) May throw any exception thrown by direct-initializing any T_i in `Types...`
3) May throw any exception thrown by move-constructing any T_i in `Types...`.
`noexcept` specification:
noexcept( (std::is_nothrow_move_constructible_v<Types> && ...))
4) May throw any exception thrown by the initialization of the selected alternative `T_j`.
`noexcept` specification:
noexcept(std::is_nothrow_constructible_v<T_j, T>)
5-8) May throw any exception thrown by calling the selected constructor of the selected alternative

### [edit] Example

| This section is incomplete Reason: no example

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

- conditionally noexcept
- Todo no example