Namespaces
Variants
Views
Actions

std::optional<T>::operator=

From cppreference.com
< cpp‎ | utility‎ | optional
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (C++20)
Dynamic memory management
Program utilities
Coroutine support (C++20)
Variadic functions
Debugging support
(C++26)
Three-way comparison
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)

 
 
(1)
optional& operator=( std::nullopt_t ) noexcept;
(since C++17)
(until C++20)
constexpr optional& operator=( std::nullopt_t ) noexcept;
(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)
(4)
template< class U = T >
optional& operator=( U&& value );
(since C++17)
(until C++20)
template< class U = T >
constexpr optional& operator=( U&& value );
(since C++20)
(5)
template< class U >
optional& operator=( const optional<U>& other );
(since C++17)
(until C++20)
template< class U >
constexpr optional& operator=( const optional<U>& other );
(since C++20)
(6)
template< class U >
optional& operator=( optional<U>&& other );
(since C++17)
(until C++20)
template< class U >
constexpr optional& operator=( optional<U>&& other );
(since C++20)

Replaces contents of *this with the contents of other.

1) If *this contains a value before the call, the contained value is destroyed by calling its destructor as if by value().T::~T(). *this does not contain a value after this call.
2,3) Assigns the state of other.
4) Perfect-forwarded assignment: depending on whether *this contains a value before the call, the contained value is either direct-initialized from std::forward<U>(value) or assigned from std::forward<U>(value). The function does not participate in overload resolution unless 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, and at least one of the following is true:
5,6) Assigns the state of other.

Contents

[edit] Parameters

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

[edit] Return value

*this

[edit] 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,3) and (5,6)) 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

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

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

[edit] Defect reports

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

DR Applied to Behavior as published Correct behavior
P0602R4 C++17 copy/move assignment operator may not be trivial
even if underlying operations are trivial
required to propagate triviality
P2231R1 C++20 converting assignment operators were not constexpr
while the required operations can be in C++20
made constexpr

[edit] See also

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