Files
2025-10-25 03:02:53 +03:00

12 KiB
Raw Permalink Blame History

[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]

1

#

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);

2

#

Constraints: is_default_constructible_v is true.

3

#

Effects: Constructs a variant holding a value-initialized value of type T0.

4

#

Postconditions: valueless_by_exception() is false and index() is 0.

5

#

Throws: Any exception thrown by the value-initialization of T0.

6

#

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);

7

#

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.

8

#

Throws: Any exception thrown by direct-initializing any Ti for all i.

9

#

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);

10

#

Constraints: is_move_constructible_v is true for all i.

11

#

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.

12

#

Throws: Any exception thrown by move-constructing any Ti for all i.

13

#

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);

14

#

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.

15

#

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]

16

#

Effects: Initializes *this to hold the alternative type Tj and direct-non-list-initializes the contained value with std::forward(t).

17

#

Postconditions: holds_alternative(*this) is true.

18

#

Throws: Any exception thrown by the initialization of the selected alternative Tj.

19

#

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);

20

#

Constraints:

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

is_constructible_v<T, Args...> is true.

21

#

Effects: Direct-non-list-initializes the contained value of type T with std::forward(args)....

22

#

Postconditions: holds_alternative(*this) is true.

23

#

Throws: Any exception thrown by calling the selected constructor of T.

24

#

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);

25

#

Constraints:

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

is_constructible_v<T, initializer_list&, Args...> is true.

26

#

Effects: Direct-non-list-initializes the contained value of type T with il, std::forward(args)....

27

#

Postconditions: holds_alternative(*this) is true.

28

#

Throws: Any exception thrown by calling the selected constructor of T.

29

#

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);

30

#

Constraints:

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

is_constructible_v<TI, Args...> is true.

31

#

Effects: Direct-non-list-initializes the contained value of type TI with std::forward(args)....

32

#

Postconditions: index() is I.

33

#

Throws: Any exception thrown by calling the selected constructor of TI.

34

#

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);

35

#

Constraints:

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

is_constructible_v<TI, initializer_list&, Args...> is true.

36

#

Effects: Direct-non-list-initializes the contained value of type TI with il, std::forward(args)....

37

#

Postconditions: index() is I.

38

#

Remarks: If TI's selected constructor is a constexpr constructor, this constructor is a constexpr constructor.