std::experimental::optional::operator= - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::experimental::optional::operator=

From cppreference.com
< cpp‎ | experimental‎ | optional

optional& operator=( std::nullopt_t ); | (1) | (library fundamentals TS)
optional& operator=( const optional& other ); | (2) | (library fundamentals TS)
optional& operator=( optional&& other ); | (3) | (library fundamentals TS)
template< class U > optional& operator=( U&& value ); | (4) | (library fundamentals TS)

Assigns contents to the contained value.

1) If *this is in engaged state before the call, the contained value is destroyed by calling its destructor. *this is in disengaged state after the call.
2-3) Assigns the state of `other`.
* If both *this and `other` are in disengaged states, the function has no effect.
* If *this is in engaged state, but `other` is not, then the contained value is destroyed by calling its destructor. *this is in disengaged state after the call.
* If `other` is in engaged state, then depending on whether *this is in engaged state, the contained value is either direct-constructed or assigned the contained value of `other`. The contained value is either copied (2), or moved (3).
4) Depending on whether *this is in engaged state before the call, the contained value is either direct-constructed from `value` or assigned `value`. `T` must be constructible and assignable from `U`, i.e. the following must hold: std::is_constructible<T,U>::value == true && std::is_assignable<T,U>::value == true

### [edit] Parameters

other | - | another `optional` object whose contained value to assign
value | - | value to assign to the contained value
Type requirements
- `T` must meet the requirements of `CopyAssignable` and `CopyConstructible` in order to use overload (2).
- `T` must meet the requirements of `MoveAssignable` and `MoveConstructible` in order to use overload (3).

### [edit] Return value

*this

### [edit] Exceptions

1)
`noexcept` specification:
noexcept
2-4) 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) ) is unchanged, i.e. if the object was in engaged state, it is left in engaged state, 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) Throws any exception thrown by the constructor or assignment operator `T`. Has the following `noexcept` declaration:
`noexcept` specification:
noexcept(std::is_nothrow_move_assignable<T>::value && std::is_nothrow_move_constructible<T>::value)

### [edit] See also

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

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/experimental/optional/operator%3D&oldid=68164"
Categories:

- unconditionally noexcept
- conditionally noexcept