std::experi...::operator=(3)
okay bestie, this is the C++ Standard Libary manual page for std::experimental::optional::operator= β manual name: std::experi...::operator=(3). Yes, it's giving assignment energy, and we're gonna make it make sense. π
πΈ NAME πΈ
std::experimental::optional::operator= β std::experimental::optional::operator=
β¨ Synopsis β¨
optional& operator=( std::experimental::nullopt_t ) (1) (library
fundamentals TS)
noexcept;
optional& operator=( const optional& other ); (2) (library
fundamentals TS)
optional& operator=( optional&& other ) noexcept(/* (3) (library
fundamentals TS)
see below */);
template< class U > (4) (library
fundamentals TS)
optional& operator=( U&& value );
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 val->T::~T(). *this does not contain a value after this call.
2,3) Assigns the state of other. The way this plays out:
- If both
*thisandotherdo not contain a value, the function has no effect. - If
*thiscontains a value, butotherdoes not, then the contained value is destroyed by calling its destructor.*thisdoes not contain a value after the call. - If
othercontains a value, then depending on whether*thiscontains a value, the contained value is either direct-initialized or assigned from*other(2) orstd::move(*other)(3). Note that a moved-from optional still contains a value.
4) Decay-only 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::is_same<std::decay_t<U>, T>::value is true.
π Parameters π
otherβ another optional object whose contained value to assignvalueβ value to assign to the contained value
π Type requirements π
Tmust meet the requirements ofCopyAssignableandCopyConstructiblein order to use overload (2).Tmust meet the requirements ofMoveAssignableandMoveConstructiblein order to use overload (3).
π«Ά Return value π«Ά
*this
π Exceptions π
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 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 the following noexcept declaration:
noexcept specification:
noexcept(std::is_nothrow_move_assignable<T>::value &&
std::is_nothrow_move_constructible<T>::value)
π¦ Notes π¦
An optional object op may be turned into an empty optional with both op = {}; and op = nullopt;.
π Example π
// Run this code
#include <experimental/optional>
#include <iostream>
int main()
{
std::experimental::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
π See also π
emplaceβ constructs the contained value in-place (public member function)
π·οΈ Categories π·οΈ
- Noindexed pages
- conditionally noexcept
that's the whole page, bestie π source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::experi...::operator=(3) β byeee π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms