23 General utilities library [utilities]
In the descriptions that follow, let
i be in the range
[0, sizeof...(Types)),
and be the type in
Types.... constexpr variant() noexcept(see below);
Effects:
Constructs a
variant holding a value-initialized value of type
. Postconditions:
valueless_by_exception() is
false and
index() is
0. Throws:
Any exception thrown by the value-initialization of
. Remarks:
This function shall be
constexpr if and only if the
value-initialization of the alternative type would satisfy the
requirements for a constexpr function
. The expression inside
noexcept is equivalent to
is_nothrow_default_constructible_v<>. This function shall not participate in overload resolution unless
is_default_constructible_v<> is
true. [
Note: See also class
monostate. —
end note ]
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<j>(w), where
j is
w.index(). Otherwise, initializes the
variant to not hold a value
.Throws:
Any exception thrown by direct-initializing any for all
i. Remarks:
This function shall not participate in overload resolution unless
is_copy_constructible_v<> is
true for all
i. variant(variant&& w) noexcept(see below);
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(). Otherwise, initializes the
variant to not hold a value
.Throws:
Any exception thrown by move-constructing any for all
i. Remarks:
The expression inside
noexcept is equivalent to the logical AND of
is_nothrow_move_constructible_v<> for all
i. This function shall not participate in overload resolution unless
is_move_constructible_v<> is
true for all
i.template <class T> constexpr variant(T&& t) noexcept(see below);
Let be a type that is determined as follows:
build an imaginary function
FUN() for each alternative type
. The overload
FUN() selected by overload
resolution for the expression
FUN(std::forward<T>(t)) defines
the alternative which is the type of the contained value after
construction
.Effects:
Initializes
*this to hold the alternative type and
direct-initializes the contained value as if direct-non-list-initializing it
with
std::forward<T>(t). Postconditions:
holds_alternative<>(*this) is
true. Throws:
Any exception thrown by the initialization of the selected alternative
. Remarks:
This function shall not participate in overload resolution unless
is_same_v<decay_t<T>, variant> is
false,
unless
decay_t<T> is neither
a specialization of
in_place_type_t
nor a specialization of
in_place_index_t,
unless
is_constructible_v<, T> is
true,
and unless the expression
FUN(
std::forward<T>(t)) (with
FUN
being the above-mentioned set of imaginary functions) is well formed
. [
Note:
variant<string, string> v("abc");
is ill-formed, as both alternative types have an equally viable constructor
for the argument
. —
end note ]
The expression inside
noexcept is equivalent to
is_nothrow_constructible_v<, T>. If 's selected constructor is a constexpr constructor,
this constructor shall be a constexpr constructor
.template <class T, class... Args> constexpr explicit variant(in_place_type_t<T>, Args&&... args);
Effects:
Initializes the contained value as if direct-non-list-initializing
an object of type
T
with the arguments
std::forward<Args>(args).... Postconditions:
holds_alternative<T>(*this) is
true. Throws:
Any exception thrown by calling the selected constructor of
T. Remarks:
This function shall not participate in overload resolution unless there is
exactly one occurrence of
T in
Types... and
is_constructible_v<T, Args...> is
true. If
T's selected constructor is a constexpr constructor, this
constructor shall be a constexpr constructor
.template <class T, class U, class... Args>
constexpr explicit variant(in_place_type_t<T>, initializer_list<U> il, Args&&... args);
Effects:
Initializes the contained value as if direct-non-list-initializing
an object of type
T
with the arguments
il, std::forward<Args>(args).... Postconditions:
holds_alternative<T>(*this) is
true. Throws:
Any exception thrown by calling the selected constructor of
T. Remarks:
This function shall not participate in overload resolution unless there is
exactly one occurrence of
T in
Types... and
is_constructible_v<T, initializer_list<U>&, Args...> is
true. If
T's selected constructor is a constexpr constructor, this
constructor shall be a constexpr constructor
.template <size_t I, class... Args> constexpr explicit variant(in_place_index_t<I>, Args&&... args);
Effects:
Initializes the contained value as if direct-non-list-initializing
an object of type
with the arguments
std::forward<Args>(args).... Postconditions:
index() is
I. Throws:
Any exception thrown by calling the selected constructor of
. Remarks:
This function shall not participate in overload resolution unless
If 's selected constructor is a constexpr constructor, this
constructor shall be 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);
Effects:
Initializes the contained value as if direct-non-list-initializing
an object of type
with the arguments
il, std::forward<Args>(args).... Postconditions:
index() is
I. Remarks:
This function shall not participate in overload resolution unless
If 's selected constructor is a constexpr constructor, this
constructor shall be a constexpr constructor
.template <class Alloc>
variant(allocator_arg_t, const Alloc& a);
template <class Alloc>
variant(allocator_arg_t, const Alloc& a, const variant& v);
template <class Alloc>
variant(allocator_arg_t, const Alloc& a, variant&& v);
template <class Alloc, class T>
variant(allocator_arg_t, const Alloc& a, T&& t);
template <class Alloc, class T, class... Args>
variant(allocator_arg_t, const Alloc& a, in_place_type_t<T>, Args&&... args);
template <class Alloc, class T, class U, class... Args>
variant(allocator_arg_t, const Alloc& a, in_place_type_t<T>,
initializer_list<U> il, Args&&... args);
template <class Alloc, size_t I, class... Args>
variant(allocator_arg_t, const Alloc& a, in_place_index_t<I>, Args&&... args);
template <class Alloc, size_t I, class U, class... Args>
variant(allocator_arg_t, const Alloc& a, in_place_index_t<I>,
initializer_list<U> il, Args&&... args);
Effects:
Equivalent to the preceding constructors except that the contained value is
constructed with uses-allocator construction (
[allocator.uses.construction])
.