Namespaces
Variants

std::optional<T>::operator=

From cppreference.com
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
optional& operator=( std::nullopt_t ) noexcept;
(1) (since C++17)
(constexpr since C++20)
constexpr optional& operator=( const optional& other );
(2) (since C++17)
constexpr optional& operator= ( optional&& other ) noexcept(/* see below */);
(3) (since C++17)
template< class U > optional& operator=( const optional<U>& other );
(4) (since C++17)
(constexpr since C++20)
template< class U > optional& operator=( optional<U>&& other );
(5) (since C++17)
(constexpr since C++20)
template< class U = std::remove_cv_t<T> > optional& operator=( U&& value );
(6) (since C++17)
(constexpr since C++20)

Replaces contents of *this with the contents of other.

1) If *this contains a value, calls val ->T::~T() to destroy the contained value; otherwise no effect. *this does not contain a value after this call.
2-5) Assigns the state of other. has_value() returns other.has_value() after this call.
Effect *this contains a value *this does not contain a value
other contains a value
  • for overloads (2,4), assigns *other to the contained value
  • for overloads (3,5), assigns std::move(*other) to the contained value
  • for overloads (2,4), direct-non-list-initializes the contained value with *other
  • for overloads (3,5), direct-non-list-initializes the contained value with std::move(*other)
other does not contain a value destroys the contained value by calling val ->T::~T() no effect
2) If std::is_copy_constructible_v<T> or std::is_copy_assignable_v<T> is false, the assignment operator is defined as deleted.
If std::is_trivially_copy_constructible_v<T>, std::is_trivially_copy_assignable_v<T> and std::is_trivially_destructible_v<T> are all true, the assignment operator is trivial.
3) This overload participates in overload resolution only if std::is_move_constructible_v<T> and std::is_move_assignable_v<T> are both true.
If std::is_trivially_move_constructible_v<T>, std::is_trivially_move_assignable_v<T> and std::is_trivially_destructible_v<T> are all true, the assignment operator is trivial.
4,5) These overloads participate in overload resolution only if all following conditions are satisfied:
  • The following 12 values are all false[1]:
    • std::is_constructible_v<T, std::optional<U>&>
    • std::is_constructible_v<T, const std::optional<U>&>
    • std::is_constructible_v<T, std::optional<U>&&>
    • std::is_constructible_v<T, const std::optional<U>&&>
    • std::is_convertible_v<std::optional<U>&, T>
    • std::is_convertible_v<const std::optional<U>&, T>
    • std::is_convertible_v<std::optional<U>&&, T>
    • std::is_convertible_v<const std::optional<U>&&, T>
    • std::is_assignable_v<T&, std::optional<U>&>
    • std::is_assignable_v<T&, const std::optional<U>&>
    • std::is_assignable_v<T&, std::optional<U>&&>
    • std::is_assignable_v<T&, const std::optional<U>&&>
  • For overload (4), std::is_constructible_v<T, const U&> and std::is_assignable_v<T&, const U&> are both true.
  • For overload (5), std::is_constructible_v<T, U> and std::is_assignable_v<T&, U> are both true.
6) If *this contains a value, assigns std::forward<U>(value) to the contained value; otherwise direct-non-list-initializes the contained value with std::forward<U>(value). *this contains a value after this call.
This overload participates in overload resolution only if all following conditions are satisfied:
  • std::decay_t<U>(until C++20)std::remove_cvref_t<U>(since C++20) is not std::optional<T>.
  • std::is_constructible_v<T, U> is true.
  • std::is_assignable_v<T&, U> is true.
  • Any of the following conditions is satisfied:
  1. In other words, T is not constructible, convertible, or assignable from any expression of type (possibly const-qualified) std::optional<U>

Parameters

other - another optional object whose contained value to assign
value - value to assign to the contained value

Return value

*this

Exceptions

2-6) Throws any exception thrown by the constructor or assignment operator of T. If an exception is thrown, the initialization state of *this (and of other in case of (2-5)) is unchanged, i.e. if the object contained a value, it still contains a value, and the other way round. The contents of value and the contained values of *this and other depend on the exception safety guarantees of the operation from which the exception originates (copy-constructor, move-assignment, etc.).
3) Has following
noexcept specification:  
noexcept(std::is_nothrow_move_assignable_v<T> && std::is_nothrow_move_constructible_v<T>)

Notes

An optional object op may be turned into an empty optional with both op = {}; and op = nullopt;. The first expression constructs an empty optional object with {} and assigns it to op.

Feature-test macro Value Std Feature
__cpp_lib_optional 202106L (C++20)
(DR20)
Fully constexpr (1), (4-6)

Example

#include <iostream>
#include <optional>

int main()
{
    std::optional<const char*> s1 = "abc", s2; // constructor
    s2 = s1; // assignment
    s1 = "def"; // decaying assignment (U = char[4], T = const char*)
    std::cout << *s2 << ' ' << *s1 << '\n';
}

Output:

abc def

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 3886 C++17 the default template argument of overload (6) was T changed to std::remove_cv_t<T>
P0602R4 C++17 copy/move assignment operator may not be trivial
even if underlying operations are trivial
required to propagate triviality
P2231R1 C++20 overloads (1,4-6) were not constexpr made constexpr

See also

constructs the contained value in-place
(public member function) [edit]