[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 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[.](#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*(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 is true for all i[.](#9.sentence-1) If is_trivially_copy_constructible_v 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 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*(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 for all i[.](#13.sentence-1) If is_trivially_move_constructible_v is true for all i, this constructor is trivial[.](#13.sentence-2) [🔗](#lib:variant,constructor___) `template 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)}; 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)) 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, variant> is false, - [(15.3)](#15.3) remove_cvref_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 is true, and - [(15.5)](#15.5) the expression *FUN*(​std​::​forward(t)) (with *FUN* being the above-mentioned set of imaginary functions) is well-formed[.](#15.sentence-1) [*Note [2](#note-2)*: variant 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)[.](#16.sentence-1) [17](#17) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5889) *Postconditions*: holds_alternative(*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[.](#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 constexpr explicit variant(in_place_type_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 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)...[.](#21.sentence-1) [22](#22) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5923) *Postconditions*: holds_alternative(*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 constexpr explicit variant(in_place_type_t, initializer_list 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&, 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)...[.](#26.sentence-1) [27](#27) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L5956) *Postconditions*: holds_alternative(*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 constexpr explicit variant(in_place_index_t, 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 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)...[.](#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 constexpr explicit variant(in_place_index_t, initializer_list 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&, 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)...[.](#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)