|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
optional::operator= | ||||
| Observers | ||||
| Iterators | ||||
(C++26) | ||||
(C++26) | ||||
| Monadic operations | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
| Modifiers | ||||
| Non-member functions | ||||
| Deduction guides | ||||
| Helper classes | ||||
| Helper objects | ||||
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 ofother.
val ->T::~T() to destroy the contained value; otherwise no effect.*this does not contain a value after this call.| Effect | *this contains a value | *this does not contain a value |
|---|---|---|
| other contains a value |
|
|
| other does not contain a value | destroys the contained value by callingval ->T::~T() | no effect |
T is not ascalar type.T.T is not constructible, convertible, or assignable from any expression of type (possibly const-qualified)std::optional<U>Contents |
| other | - | anotheroptional object whose contained value to assign |
| value | - | value to assign to the contained value |
*this
T. If an exception is thrown, the initialization state of*this (and ofother 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 ofvalue and the contained values of*this andother depend on the exception safety guarantees of the operation from which the exception originates (copy-constructor, move-assignment, etc.).An optional objectop may be turned into an empty optional with bothop={}; andop= nullopt;. The first expression constructs an emptyoptional object with{} and assigns it toop.
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_optional | 202106L | (C++20) (DR20) | Fullyconstexpr(1),(4-6) |
#include <iostream>#include <optional> int main(){std::optional<constchar*> 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
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) wasT | changed tostd::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 notconstexpr | madeconstexpr |
| constructs the contained value in-place (public member function)[edit] |