12 KiB
[variant.ctor]
22 General utilities library [utilities]
22.6 Variants [variant]
22.6.3 Class template variant [variant.variant]
22.6.3.2 Constructors [variant.ctor]
In the descriptions that follow, let i be in the range [0, sizeof...(Types)), and Ti be the ith type in Types.
constexpr variant() noexcept(see below);
Constraints: is_default_constructible_v is true.
Effects: Constructs a variant holding a value-initialized value of type T0.
Postconditions: valueless_by_exception() is false and index() is 0.
Throws: Any exception thrown by the value-initialization of T0.
Remarks: This function is constexpr if and only if the value-initialization of the alternative type T0 would be constexpr-suitable ([dcl.constexpr]).
The exception specification is equivalent tois_nothrow_default_constructible_v.
[Note 1:
See also class monostate.
â end note]
constexpr variant(const variant& w);
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().
Otherwise, initializes the variant to not hold a value.
Throws: Any exception thrown by direct-initializing any Ti for all i.
Remarks: This constructor is defined as deleted unlessis_copy_constructible_v is true for all i.
If is_trivially_copy_constructible_v is true for all i, this constructor is trivial.
constexpr variant(variant&& w) noexcept(see below);
Constraints: is_move_constructible_v is true for all i.
Effects: If w holds a value, initializes the variant to hold the same alternative as w and direct-initializes the contained value withGET(std::move(w)), where j is w.index().
Otherwise, initializes the variant to not hold a value.
Throws: Any exception thrown by move-constructing any Ti for all i.
Remarks: The exception specification is equivalent to the logical and ofis_nothrow_move_constructible_v for all i.
If is_trivially_move_constructible_v is true for all i, this constructor is trivial.
template<class T> constexpr variant(T&& t) noexcept(see below);
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.
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.
Constraints:
sizeof...(Types) is nonzero,
is_same_v<remove_cvref_t, variant> is false,
remove_cvref_t is neither a specialization of in_place_type_t nor a specialization of in_place_index_t,
is_constructible_v<Tj, T> is true, and
the expression FUN(std::forward(t)) (with FUN being the above-mentioned set of imaginary functions) is well-formed. [Note 2: variant<string, string> v("abc"); is ill-formed, as both alternative types have an equally viable constructor for the argument. â end note]
Effects: Initializes *this to hold the alternative type Tj and direct-non-list-initializes the contained value with std::forward(t).
Postconditions: holds_alternative(*this) is true.
Throws: Any exception thrown by the initialization of the selected alternative Tj.
Remarks: The exception specification is equivalent tois_nothrow_constructible_v<Tj, T>.
If Tj's selected constructor is a constexpr constructor, this constructor is a constexpr constructor.
template<class T, class... Args> constexpr explicit variant(in_place_type_t<T>, Args&&... args);
Constraints:
There is exactly one occurrence of T in Types... and
is_constructible_v<T, Args...> is true.
Effects: Direct-non-list-initializes the contained value of type T with std::forward(args)....
Postconditions: holds_alternative(*this) is true.
Throws: Any exception thrown by calling the selected constructor of T.
Remarks: If T's selected constructor is a constexpr constructor, this constructor is a constexpr constructor.
template<class T, class U, class... Args> constexpr explicit variant(in_place_type_t<T>, initializer_list<U> il, Args&&... args);
Constraints:
There is exactly one occurrence of T in Types... and
is_constructible_v<T, initializer_list&, Args...> is true.
Effects: Direct-non-list-initializes the contained value of type T with il, std::forward(args)....
Postconditions: holds_alternative(*this) is true.
Throws: Any exception thrown by calling the selected constructor of T.
Remarks: If T's selected constructor is a constexpr constructor, this constructor is a constexpr constructor.
template<size_t I, class... Args> constexpr explicit variant(in_place_index_t<I>, Args&&... args);
Constraints:
I is less than sizeof...(Types) and
is_constructible_v<TI, Args...> is true.
Effects: Direct-non-list-initializes the contained value of type TI with std::forward(args)....
Postconditions: index() is I.
Throws: Any exception thrown by calling the selected constructor of TI.
Remarks: If TI's selected constructor is a constexpr constructor, this constructor is a constexpr constructor.
template<size_t I, class U, class... Args> constexpr explicit variant(in_place_index_t<I>, initializer_list<U> il, Args&&... args);
Constraints:
I is less than sizeof...(Types) and
is_constructible_v<TI, initializer_list&, Args...> is true.
Effects: Direct-non-list-initializes the contained value of type TI with il, std::forward(args)....
Postconditions: index() is I.
Remarks: If TI's selected constructor is a constexpr constructor, this constructor is a constexpr constructor.