[variant.ctor]

# 22 General utilities library [[utilities]](./#utilities)

## 22.6 Variants [[variant]](variant#ctor)

### 22.6.3 Class template variant [[variant.variant]](variant.variant#variant.ctor)

#### 22.6.3.2 Constructors [variant.ctor]

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5745)

In the descriptions that follow, let i be in the range [0, sizeof...(Types)),
and Ti be the ith type in Types[.](#1.sentence-1)

[ð](#lib:variant,constructor)

`constexpr variant() noexcept(see below);
`

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5755)

*Constraints*: is_default_constructible_v<T0> is true[.](#2.sentence-1)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5759)

*Effects*: Constructs a variant holding a value-initialized value of type T0[.](#3.sentence-1)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5763)

*Postconditions*: valueless_by_exception() is false and index() is 0[.](#4.sentence-1)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5767)

*Throws*: Any exception thrown by the value-initialization of T0[.](#5.sentence-1)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5771)

*Remarks*: This function is constexpr if and only if the
value-initialization of the alternative type T0 would be constexpr-suitable ([[dcl.constexpr]](dcl.constexpr "9.2.6 The constexpr and consteval specifiers"))[.](#6.sentence-1)

The exception specification is equivalent tois_nothrow_default_constructible_v<T0>[.](#6.sentence-2)

[*Note [1](#note-1)*:

See also class monostate[.](#6.sentence-3)

â *end note*]

[ð](#lib:variant,constructor_)

`constexpr variant(const variant& w);
`

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5789)

*Effects*: If w holds a value, initializes the variant to hold the same
alternative as w and direct-initializes the contained value
with *GET*<j>(w), where j is w.index()[.](#7.sentence-1)

Otherwise, initializes the variant to not hold a value[.](#7.sentence-2)

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5796)

*Throws*: Any exception thrown by direct-initializing any Ti for all i[.](#8.sentence-1)

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5800)

*Remarks*: This constructor is defined as deleted unlessis_copy_constructible_v<Ti> is true for all i[.](#9.sentence-1)

If is_trivially_copy_constructible_v<Ti> is true for all i, this constructor is trivial[.](#9.sentence-2)

[ð](#lib:variant,constructor__)

`constexpr variant(variant&& w) noexcept(see below);
`

[10](#10)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5814)

*Constraints*: is_move_constructible_v<Ti> is true for all i[.](#10.sentence-1)

[11](#11)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5818)

*Effects*: If w holds a value, initializes the variant to hold the same
alternative as w and direct-initializes the contained value with*GET*<j>(std​::​move(w)), where j is w.index()[.](#11.sentence-1)

Otherwise, initializes the variant to not hold a value[.](#11.sentence-2)

[12](#12)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5825)

*Throws*: Any exception thrown by move-constructing any Ti for all i[.](#12.sentence-1)

[13](#13)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5829)

*Remarks*: The exception specification is equivalent to the logical and ofis_nothrow_move_constructible_v<Ti> for all i[.](#13.sentence-1)

If is_trivially_move_constructible_v<Ti> is true for all i, this constructor is trivial[.](#13.sentence-2)

[ð](#lib:variant,constructor___)

`template<class T> constexpr variant(T&& t) noexcept(see below);
`

[14](#14)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5843)

Let Tj be a type that is determined as follows:
build an imaginary function *FUN*(Ti) for each alternative type Ti for which Ti x[] = {std​::​forward<T>(t)}; is well-formed for some invented variable x[.](#14.sentence-1)

The overload *FUN*(Tj) selected by overload
resolution for the expression *FUN*(std​::​forward<T>(​t)) defines
the alternative Tj which is the type of the contained value after
construction[.](#14.sentence-2)

[15](#15)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5854)

*Constraints*: 

- [(15.1)](#15.1)

sizeof...(Types) is nonzero,

- [(15.2)](#15.2)

is_same_v<remove_cvref_t<T>, variant> is false,

- [(15.3)](#15.3)

remove_cvref_t<T> is neither
a specialization of in_place_type_t nor
a specialization of in_place_index_t,

- [(15.4)](#15.4)

is_constructible_v<Tj, T> is true, and

- [(15.5)](#15.5)

the expression *FUN*(​std​::​forward<T>(t)) (with *FUN* being the above-mentioned set of
imaginary functions) is well-formed[.](#15.sentence-1)
  [*Note [2](#note-2)*:
  variant<string, string> v("abc"); is ill-formed, as both alternative types have an equally viable constructor
for the argument[.](#15.5.sentence-2)
 â *end note*]

[16](#16)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5884)

*Effects*: Initializes *this to hold the alternative type Tj and
direct-non-list-initializes the contained value with std​::​forward<T>(t)[.](#16.sentence-1)

[17](#17)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5889)

*Postconditions*: holds_alternative<Tj>(*this) is true[.](#17.sentence-1)

[18](#18)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5893)

*Throws*: Any exception thrown by the initialization of the selected alternative Tj[.](#18.sentence-1)

[19](#19)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5897)

*Remarks*: The exception specification is equivalent tois_nothrow_constructible_v<Tj, T>[.](#19.sentence-1)

If Tj's selected constructor is a constexpr constructor,
this constructor is a constexpr constructor[.](#19.sentence-2)

[ð](#lib:variant,constructor____)

`template<class T, class... Args> constexpr explicit variant(in_place_type_t<T>, Args&&... args);
`

[20](#20)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5911)

*Constraints*: 

- [(20.1)](#20.1)

There is exactly one occurrence of T in Types... and

- [(20.2)](#20.2)

is_constructible_v<T, Args...> is true[.](#20.sentence-1)

[21](#21)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5918)

*Effects*: Direct-non-list-initializes the contained value of type T with std​::​forward<Args>(args)...[.](#21.sentence-1)

[22](#22)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5923)

*Postconditions*: holds_alternative<T>(*this) is true[.](#22.sentence-1)

[23](#23)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5927)

*Throws*: Any exception thrown by calling the selected constructor of T[.](#23.sentence-1)

[24](#24)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5931)

*Remarks*: If T's selected constructor is a constexpr constructor, this
constructor is a constexpr constructor[.](#24.sentence-1)

[ð](#lib:variant,constructor_____)

`template<class T, class U, class... Args>
  constexpr explicit variant(in_place_type_t<T>, initializer_list<U> il, Args&&... args);
`

[25](#25)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5944)

*Constraints*: 

- [(25.1)](#25.1)

There is exactly one occurrence of T in Types... and

- [(25.2)](#25.2)

is_constructible_v<T, initializer_list<U>&, Args...> is true[.](#25.sentence-1)

[26](#26)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5951)

*Effects*: Direct-non-list-initializes the contained value of type T with il, std​::​forward<Args>(​args)...[.](#26.sentence-1)

[27](#27)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5956)

*Postconditions*: holds_alternative<T>(*this) is true[.](#27.sentence-1)

[28](#28)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5960)

*Throws*: Any exception thrown by calling the selected constructor of T[.](#28.sentence-1)

[29](#29)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5964)

*Remarks*: If T's selected constructor is a constexpr constructor, this
constructor is a constexpr constructor[.](#29.sentence-1)

[ð](#lib:variant,constructor______)

`template<size_t I, class... Args> constexpr explicit variant(in_place_index_t<I>, Args&&... args);
`

[30](#30)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5976)

*Constraints*: 

- [(30.1)](#30.1)

I is less than sizeof...(Types) and

- [(30.2)](#30.2)

is_constructible_v<TI, Args...> is true[.](#30.sentence-1)

[31](#31)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5985)

*Effects*: Direct-non-list-initializes the contained value of type TI with std​::​forward<Args>(args)...[.](#31.sentence-1)

[32](#32)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5990)

*Postconditions*: index() is I[.](#32.sentence-1)

[33](#33)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5994)

*Throws*: Any exception thrown by calling the selected constructor of TI[.](#33.sentence-1)

[34](#34)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5998)

*Remarks*: If TI's selected constructor is a constexpr constructor, this
constructor is a constexpr constructor[.](#34.sentence-1)

[ð](#lib:variant,constructor_______)

`template<size_t I, class U, class... Args>
  constexpr explicit variant(in_place_index_t<I>, initializer_list<U> il, Args&&... args);
`

[35](#35)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6011)

*Constraints*: 

- [(35.1)](#35.1)

I is less than sizeof...(Types) and

- [(35.2)](#35.2)

is_constructible_v<TI, initializer_list<U>&, Args...> is true[.](#35.sentence-1)

[36](#36)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6020)

*Effects*: Direct-non-list-initializes the contained value of type TI with il, std​::​forward<Args>(​args)...[.](#36.sentence-1)

[37](#37)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6025)

*Postconditions*: index() is I[.](#37.sentence-1)

[38](#38)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6029)

*Remarks*: If TI's selected constructor is a constexpr constructor, this
constructor is a constexpr constructor[.](#38.sentence-1)
