Header file optional.hpp

Class tl::monostate

Used to represent an optional with no data; essentially a bool

Struct tl::in_place_t

A tag type to tell optional to construct its value in-place

Variable tl::in_place

A tag to tell optional to construct its value in-place

Struct tl::nullopt_t

A tag type to represent an empty optional

Variable 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

Class template 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.

Function template 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.

Function template 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.

Function template 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)().

Function template 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.

Function template 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.

Function template tl::optional::conjunction

Returns: u if *this has a value, otherwise an empty optional.

Function tl::optional::disjunction

Returns: rhs if *this is empty, otherwise the current value.

Function tl::optional::take

Takes the value out of the optional, leaving it empty

Default constructor tl::optional::optional

Constructs an optional that does not contain a value.

Copy constructor tl::optional::optional

Copy constructor

If rhs contains a value, the stored value is direct-initialized with it. Otherwise, the constructed optional is empty.

Move constructor tl::optional::optional

Move constructor

If rhs contains a value, the stored value is direct-initialized with it. Otherwise, the constructed optional is empty.

Function template tl::optional::optional

Constructs the stored value in-place using the given arguments.

Function template tl::optional::optional

Constructs the stored value with u.

Function template tl::optional::optional

Converting copy constructor.

Function template tl::optional::optional

Converting move constructor.

Destructor tl::optional::~optional

Destroys the stored value if there is one.

Assignment operator tl::optional::operator=

Assignment to empty.

Destroys the current value if there is one.

Assignment operator tl::optional::operator=

Copy assignment.

Copies the value from rhs if there is one. Otherwise resets the stored value in *this.

Assignment operator tl::optional::operator=

Move assignment.

Moves the value from rhs if there is one. Otherwise resets the stored value in *this.

Assignment operator tl::optional::operator=

Assigns the stored value from u, destroying the old value if there was one.

Assignment operator tl::optional::operator=

Converting copy assignment operator.

Copies the value from rhs if there is one. Otherwise resets the stored value in *this.

Assignment operator tl::optional::operator=

Converting move assignment operator.

Moves the value from rhs if there is one. Otherwise resets the stored value in *this.

Function template tl::optional::emplace

Constructs the value in-place, destroying the current one if there is one.

Function 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.

Operator tl::optional::operator->

Returns: a pointer to the stored value

Requires: a value is stored

Operator tl::optional::operator*

Returns: the stored value

Requires: a value is stored

Function tl::optional::has_value

Returns: whether or not the optional has a value

Function tl::optional::value

Returns: the contained value if there is one, otherwise throws [bad_optional_access]

Function template tl::optional::value_or

Returns: the stored value if there is one, otherwise returns u

Function tl::optional::reset

Destroys the stored value if one exists, making the optional empty


Comparison operator tl::operator==

Compares two optional objects

If both optionals contain a value, they are compared with Ts 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.

Comparison operator tl::operator==

Compares an optional to a nullopt

Equivalent to comparing the optional to an empty optional

Comparison operator tl::operator==

Compares the optional with a value.

If the optional has a value, it is compared with the other value using Ts relational operators. Otherwise, the optional is considered less than the value.

Class template 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

Function template 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.

Function template 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.

Function template 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)().

Function template 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.

Function template 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.

Function template tl::optional<T&>::conjunction

Returns: u if *this has a value, otherwise an empty optional.

Function tl::optional<T&>::disjunction

Returns: rhs if *this is empty, otherwise the current value.

Function tl::optional<T&>::take

Takes the value out of the optional, leaving it empty

Default constructor tl::optional<T&>::optional

Constructs an optional that does not contain a value.

Copy constructor 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.

Move constructor 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.

Function template tl::optional<T&>::optional

Constructs the stored value with u.

Destructor tl::optional<T&>::~optional

No-op

Assignment operator tl::optional<T&>::operator=

Assignment to empty.

Destroys the current value if there is one.

Copy assignment operator 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.

Assignment operator tl::optional<T&>::operator=

Rebinds this optional to u.

Requires: U must be an lvalue reference.

Assignment operator 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.

Function template tl::optional<T&>::emplace

Constructs the value in-place, destroying the current one if there is one.

Function 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.

Operator tl::optional<T&>::operator->

Returns: a pointer to the stored value

Requires: a value is stored

Operator tl::optional<T&>::operator*

Returns: the stored value

Requires: a value is stored

Function tl::optional<T&>::has_value

Returns: whether or not the optional has a value

Function tl::optional<T&>::value

Returns: the contained value if there is one, otherwise throws [bad_optional_access]

synopsis constexpr T &value();

Function template tl::optional<T&>::value_or

Returns: the stored value if there is one, otherwise returns u

Function tl::optional<T&>::reset

Destroys the stored value if one exists, making the optional empty