22 General utilities library [utilities]

22.5 Optional objects [optional]

22.5.3 Class template optional [optional.optional]

22.5.3.4 Assignment [optional.assign]

constexpr optional<T>& operator=(nullopt_t) noexcept;
Returns: *this.
constexpr optional<T>& operator=(const optional& rhs);
Remarks: If any exception is thrown, the result of the expression this->has_value() remains unchanged.
If an exception is thrown during the call to T's copy constructor, no effect.
If an exception is thrown during the call to T's copy assignment, the state of its contained value is as defined by the exception safety guarantee of T's copy assignment.
This operator is defined as deleted unless is_copy_constructible_v<T> is true and is_copy_assignable_v<T> is true.
If is_trivially_copy_constructible_v<T> && is_trivially_copy_assignable_v<T> && is_trivially_destructible_v<T> is true, this assignment operator is trivial.
constexpr optional& operator=(optional&& rhs) noexcept(see below);
Constraints: is_move_constructible_v<T> is true and is_move_assignable_v<T> is true.
Remarks: The exception specification is equivalent to: is_nothrow_move_assignable_v<T> && is_nothrow_move_constructible_v<T>
If any exception is thrown, the result of the expression this->has_value() remains unchanged.
If an exception is thrown during the call to T's move constructor, the state of rhs.val is determined by the exception safety guarantee of T's move constructor.
If an exception is thrown during the call to T's move assignment, the states of val and rhs.val are determined by the exception safety guarantee of T's move assignment.
If is_trivially_move_constructible_v<T> && is_trivially_move_assignable_v<T> && is_trivially_destructible_v<T> is true, this assignment operator is trivial.
template<class U = remove_cv_t<T>> constexpr optional& operator=(U&& v);
Effects: If *this contains a value, assigns std​::​forward<U>(v) to val; otherwise direct-non-list-initializes val with std​::​forward<U>(v).
Remarks: If any exception is thrown, the result of the expression this->has_value() remains unchanged.
If an exception is thrown during the call to T's constructor, the state of v is determined by the exception safety guarantee of T's constructor.
If an exception is thrown during the call to T's assignment, the states of val and v are determined by the exception safety guarantee of T's assignment.
template<class U> constexpr optional<T>& operator=(const optional<U>& rhs);
Returns: *this.
Remarks: If any exception is thrown, the result of the expression this->has_value() remains unchanged.
If an exception is thrown during the call to T's constructor, the state of rhs.val is determined by the exception safety guarantee of T's constructor.
If an exception is thrown during the call to T's assignment, the states of val and rhs.val are determined by the exception safety guarantee of T's assignment.
template<class U> constexpr optional<T>& operator=(optional<U>& rhs);
Returns: *this.
Remarks: If any exception is thrown, the result of the expression this->has_value() remains unchanged.
If an exception is thrown during the call to T's constructor, the state of rhs.val is determined by the exception safety guarantee of T's constructor.
If an exception is thrown during the call to T's assignment, the states of val and rhs.val are determined by the exception safety guarantee of T's assignment.
template<class. Args> constexpr T& emplace(Args&&. args);
Effects: Calls *this = nullopt.
Then direct-non-list-initializes val with std​::​forward<Args>(args​)..
Remarks: If an exception is thrown during the call to T's constructor, *this does not contain a value, and the previous val (if any) has been destroyed.
template<class U, class. Args> constexpr T& emplace(initializer_list<U> il, Args&&. args);
Effects: Calls *this = nullopt.
Then direct-non-list-initializes val with il, std​::​forward<Args>(​args)..
Remarks: If an exception is thrown during the call to T's constructor, *this does not contain a value, and the previous val (if any) has been destroyed.

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant