optional.hpp
tl::monostate
Used to represent an optional with no data; essentially a bool
tl::in_place_t
A tag type to tell optional to construct its value in-place
tl::in_place
A tag to tell optional to construct its value in-place
tl::nullopt_t
A tag type to represent an empty optional
tl::nullopt
Represents an empty optional
Examples:
tl::optional<int> a = tl::nullopt; void foo (tl::optional<int>); foo(tl::nullopt); //pass an empty optional
tl::optional
An optional object is an object that contains the storage for another object and manages the lifetime of this contained object, if any. The contained object may be initialized after the optional object has been initialized, and may be destroyed before the optional object has been destroyed. The initialization state of the contained object is tracked by the optional object.
tl::optional::and_then
Carries out some operation which returns an optional on the stored object if there is one. \requires std::invoke(std::forward<F>(f), value())
returns a std::optional<U>
for some U
. \returns Let U
be the result of std::invoke(std::forward<F>(f), value())
. Returns a std::optional<U>
. The return value is empty if *this
is empty, otherwise the return value of std::invoke(std::forward<F>(f), value())
is returned.
tl::optional::map
Carries out some operation on the stored object if there is one.
Returns: Let U
be the result of std::invoke(std::forward<F>(f), value())
. Returns a std::optional<U>
. The return value is empty if *this
is empty, otherwise an optional<U>
is constructed from the return value of std::invoke(std::forward<F>(f), value())
and is returned.
tl::optional::or_else
Calls f
if the optional is empty
Requires: std::invoke_result_t<F>
must be void or convertible to optional<T>
.
Effects: If *this
has a value, returns *this
. Otherwise, if f
returns void
, calls std::forward<F>(f)
and returns std::nullopt
. Otherwise, returns std::forward<F>(f)()
.
tl::optional::map_or
Maps the stored value with f
if there is one, otherwise returns u
.
If there is a value stored, then f
is called with **this
and the value is returned. Otherwise u
is returned.
tl::optional::map_or_else
Maps the stored value with f
if there is one, otherwise calls u
and returns the result.
If there is a value stored, then f
is called with **this
and the value is returned. Otherwise std::forward<U>(u)()
is returned.
tl::optional::conjunction
Returns: u
if *this
has a value, otherwise an empty optional.
tl::optional::disjunction
Returns: rhs
if *this
is empty, otherwise the current value.
tl::optional::take
Takes the value out of the optional, leaving it empty
tl::optional::optional
Constructs an optional that does not contain a value.
tl::optional::optional
Copy constructor
If rhs
contains a value, the stored value is direct-initialized with it. Otherwise, the constructed optional is empty.
tl::optional::optional
Move constructor
If rhs
contains a value, the stored value is direct-initialized with it. Otherwise, the constructed optional is empty.
tl::optional::optional
Constructs the stored value in-place using the given arguments.
tl::optional::optional
Constructs the stored value with u
.
tl::optional::optional
Converting copy constructor.
tl::optional::optional
Converting move constructor.
tl::optional::~optional
Destroys the stored value if there is one.
tl::optional::operator=
Assignment to empty.
Destroys the current value if there is one.
tl::optional::operator=
Copy assignment.
Copies the value from rhs
if there is one. Otherwise resets the stored value in *this
.
tl::optional::operator=
Move assignment.
Moves the value from rhs
if there is one. Otherwise resets the stored value in *this
.
tl::optional::operator=
Assigns the stored value from u
, destroying the old value if there was one.
tl::optional::operator=
Converting copy assignment operator.
Copies the value from rhs
if there is one. Otherwise resets the stored value in *this
.
tl::optional::operator=
Converting move assignment operator.
Moves the value from rhs
if there is one. Otherwise resets the stored value in *this
.
tl::optional::emplace
Constructs the value in-place, destroying the current one if there is one.
tl::optional::swap
Swaps this optional with the other.
If neither optionals have a value, nothing happens. If both have a value, the values are swapped. If one has a value, it is moved to the other and the movee is left valueless.
tl::optional::operator->
Returns: a pointer to the stored value
Requires: a value is stored
tl::optional::operator*
Returns: the stored value
Requires: a value is stored
tl::optional::has_value
Returns: whether or not the optional has a value
tl::optional::value
Returns: the contained value if there is one, otherwise throws [bad_optional_access]
tl::optional::value_or
Returns: the stored value if there is one, otherwise returns u
tl::optional::reset
Destroys the stored value if one exists, making the optional empty
tl::operator==
Compares two optional objects
If both optionals contain a value, they are compared with T
s relational operators. Otherwise lhs
and rhs
are equal only if they are both empty, and lhs
is less than rhs
only if rhs
is empty and lhs
is not.
tl::operator==
Compares an optional to a nullopt
Equivalent to comparing the optional to an empty optional
tl::operator==
Compares the optional with a value.
If the optional has a value, it is compared with the other value using T
s relational operators. Otherwise, the optional is considered less than the value.
tl::optional<T&>
Specialization for when T
is a reference. optional<T&>
acts similarly to a T*
, but provides more operations and shows intent more clearly.
Examples:
int i = 42; tl::optional<int&> o = i; *o == 42; //true i = 12; *o = 12; //true &*o == &i; //true
Assignment has rebind semantics rather than assign-through semantics:
int j = 8; o = j; &*o == &j; //true
tl::optional<T&>::and_then
Carries out some operation which returns an optional on the stored object if there is one. \requires std::invoke(std::forward<F>(f), value())
returns a std::optional<U>
for some U
. \returns Let U
be the result of std::invoke(std::forward<F>(f), value())
. Returns a std::optional<U>
. The return value is empty if *this
is empty, otherwise the return value of std::invoke(std::forward<F>(f), value())
is returned.
tl::optional<T&>::map
Carries out some operation on the stored object if there is one.
Returns: Let U
be the result of std::invoke(std::forward<F>(f), value())
. Returns a std::optional<U>
. The return value is empty if *this
is empty, otherwise an optional<U>
is constructed from the return value of std::invoke(std::forward<F>(f), value())
and is returned.
tl::optional<T&>::or_else
Calls f
if the optional is empty
Requires: std::invoke_result_t<F>
must be void or convertible to optional<T>
. \effects If *this
has a value, returns *this
. Otherwise, if f
returns void
, calls std::forward<F>(f)
and returns std::nullopt
. Otherwise, returns std::forward<F>(f)()
.
tl::optional<T&>::map_or
Maps the stored value with f
if there is one, otherwise returns u
.
If there is a value stored, then f
is called with **this
and the value is returned. Otherwise u
is returned.
tl::optional<T&>::map_or_else
Maps the stored value with f
if there is one, otherwise calls u
and returns the result.
If there is a value stored, then f
is called with **this
and the value is returned. Otherwise std::forward<U>(u)()
is returned.
tl::optional<T&>::conjunction
Returns: u
if *this
has a value, otherwise an empty optional.
tl::optional<T&>::disjunction
Returns: rhs
if *this
is empty, otherwise the current value.
tl::optional<T&>::take
Takes the value out of the optional, leaving it empty
tl::optional<T&>::optional
Constructs an optional that does not contain a value.
tl::optional<T&>::optional
Copy constructor
If rhs
contains a value, the stored value is direct-initialized with it. Otherwise, the constructed optional is empty.
tl::optional<T&>::optional
Move constructor
If rhs
contains a value, the stored value is direct-initialized with it. Otherwise, the constructed optional is empty.
tl::optional<T&>::optional
Constructs the stored value with u
.
tl::optional<T&>::~optional
No-op
tl::optional<T&>::operator=
Assignment to empty.
Destroys the current value if there is one.
tl::optional<T&>::operator=
Copy assignment.
Rebinds this optional to the referee of rhs
if there is one. Otherwise resets the stored value in *this
.
tl::optional<T&>::operator=
Rebinds this optional to u
.
Requires: U
must be an lvalue reference.
tl::optional<T&>::operator=
Converting copy assignment operator.
Rebinds this optional to the referee of rhs
if there is one. Otherwise resets the stored value in *this
.
tl::optional<T&>::emplace
Constructs the value in-place, destroying the current one if there is one.
tl::optional<T&>::swap
Swaps this optional with the other.
If neither optionals have a value, nothing happens. If both have a value, the values are swapped. If one has a value, it is moved to the other and the movee is left valueless.
tl::optional<T&>::operator->
Returns: a pointer to the stored value
Requires: a value is stored
tl::optional<T&>::operator*
Returns: the stored value
Requires: a value is stored
tl::optional<T&>::has_value
Returns: whether or not the optional has a value
tl::optional<T&>::value
Returns: the contained value if there is one, otherwise throws [bad_optional_access]
synopsis constexpr T &value();
tl::optional<T&>::value_or
Returns: the stored value if there is one, otherwise returns u
tl::optional<T&>::reset
Destroys the stored value if one exists, making the optional empty