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

10 KiB
Raw Permalink Blame History

[optional.ctor]

22 General utilities library [utilities]

22.5 Optional objects [optional]

22.5.3 Class template optional [optional.optional]

22.5.3.2 Constructors [optional.ctor]

1

#

The exposition-only variable template converts-from-any-cvref is used by some constructors for optional.

template<class T, class W>constexpr bool converts-from-any-cvref = // exposition only disjunction_v<is_constructible<T, W&>, is_convertible<W&, T>, is_constructible<T, W>, is_convertible<W, T>, is_constructible<T, const W&>, is_convertible<const W&, T>, is_constructible<T, const W>, is_convertible<const W, T>>;

🔗

constexpr optional() noexcept; constexpr optional(nullopt_t) noexcept;

2

#

Postconditions: *this does not contain a value.

3

#

Remarks: No contained value is initialized.

For every object type T these constructors are constexpr constructors ([dcl.constexpr]).

🔗

constexpr optional(const optional& rhs);

4

#

Effects: If rhs contains a value, direct-non-list-initializes the contained value with *rhs.

5

#

Postconditions: rhs.has_value() == this->has_value().

6

#

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

7

#

Remarks: This constructor is defined as deleted unlessis_copy_constructible_v is true.

If is_trivially_copy_constructible_v is true, this constructor is trivial.

🔗

constexpr optional(optional&& rhs) noexcept(see below);

8

#

Constraints: is_move_constructible_v is true.

9

#

Effects: If rhs contains a value, direct-non-list-initializes the contained value with *std::move(rhs).

rhs.has_value() is unchanged.

10

#

Postconditions: rhs.has_value() == this->has_value().

11

#

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

12

#

Remarks: The exception specification is equivalent tois_nothrow_move_constructible_v.

If is_trivially_move_constructible_v is true, this constructor is trivial.

🔗

template<class... Args> constexpr explicit optional(in_place_t, Args&&... args);

13

#

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

14

#

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

15

#

Postconditions: *this contains a value.

16

#

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

17

#

Remarks: If T's constructor selected for the initialization is a constexpr constructor, this constructor is a constexpr constructor.

🔗

template<class U, class... Args> constexpr explicit optional(in_place_t, initializer_list<U> il, Args&&... args);

18

#

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

19

#

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

20

#

Postconditions: *this contains a value.

21

#

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

22

#

Remarks: If T's constructor selected for the initialization is a constexpr constructor, this constructor is a constexpr constructor.

🔗

template<class U = remove_cv_t<T>> constexpr explicit(see below) optional(U&& v);

23

#

Constraints:

is_constructible_v<T, U> is true,

is_same_v<remove_cvref_t, in_place_t> is false,

is_same_v<remove_cvref_t, optional> is false, and

if T is cv bool,remove_cvref_t is not a specialization of optional.

24

#

Effects: Direct-non-list-initializes the contained value with std::forward(v).

25

#

Postconditions: *this contains a value.

26

#

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

27

#

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

The expression inside explicit is equivalent to:!is_convertible_v<U, T>

🔗

template<class U> constexpr explicit(see below) optional(const optional<U>& rhs);

28

#

Constraints:

is_constructible_v<T, const U&> is true, and

if T is not cv bool,converts-from-any-cvref<T, optional> is false.

29

#

Effects: If rhs contains a value, direct-non-list-initializes the contained value with *rhs.

30

#

Postconditions: rhs.has_value() == this->has_value().

31

#

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

32

#

Remarks: The expression inside explicit is equivalent to:!is_convertible_v<const U&, T>

🔗

template<class U> constexpr explicit(see below) optional(optional<U>&& rhs);

33

#

Constraints:

is_constructible_v<T, U> is true, and

if T is not cv bool,converts-from-any-cvref<T, optional> is false.

34

#

Effects: If rhs contains a value, direct-non-list-initializes the contained value with *std::move(rhs).

rhs.has_value() is unchanged.

35

#

Postconditions: rhs.has_value() == this->has_value().

36

#

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

37

#

Remarks: The expression inside explicit is equivalent to:!is_convertible_v<U, T>