optional

Header file optional.hpp

#define TL_OPTIONAL_HPP

#define TL_OPTIONAL_VERSION_MAJOR

#define TL_OPTIONAL_VERSION_MINOR

#define IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T)

#define IS_TRIVIALLY_COPY_ASSIGNABLE(T)

#define IS_TRIVIALLY_DESTRUCTIBLE(T)

#define TL_OPTIONAL_CXX14

#define TL_MONOSTATE_INPLACE_MUTEX

#define TL_TRAITS_MUTEX

namespace tl
{
    class monostate;
    
    struct in_place_t;
    
    constexpr in_place_t{} in_place;
    
    struct nullopt_t;
    
    static constexpr nullopt_t nullopt;
    
    class bad_optional_access;
    
    template <class T>
    class optional;
    
    template <class T, class U>
    constexpr bool operator==(const optional<T>& lhs, const optional<U>& rhs);
    template <class T, class U>
    constexpr bool operator!=(const optional<T>& lhs, const optional<U>& rhs);
    template <class T, class U>
    constexpr bool operator<(const optional<T>& lhs, const optional<U>& rhs);
    template <class T, class U>
    constexpr bool operator>(const optional<T>& lhs, const optional<U>& rhs);
    template <class T, class U>
    constexpr bool operator<=(const optional<T>& lhs, const optional<U>& rhs);
    template <class T, class U>
    constexpr bool operator>=(const optional<T>& lhs, const optional<U>& rhs);
    
    template <class T>
    constexpr bool operator==(const optional<T>& lhs, nullopt_t) noexcept;
    template <class T>
    constexpr bool operator==(nullopt_t, const optional<T>& rhs) noexcept;
    template <class T>
    constexpr bool operator!=(const optional<T>& lhs, nullopt_t) noexcept;
    template <class T>
    constexpr bool operator!=(nullopt_t, const optional<T>& rhs) noexcept;
    template <class T>
    constexpr bool operator<(const optional<T>&, nullopt_t) noexcept;
    template <class T>
    constexpr bool operator<(nullopt_t, const optional<T>& rhs) noexcept;
    template <class T>
    constexpr bool operator<=(const optional<T>& lhs, nullopt_t) noexcept;
    template <class T>
    constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept;
    template <class T>
    constexpr bool operator>(const optional<T>& lhs, nullopt_t) noexcept;
    template <class T>
    constexpr bool operator>(nullopt_t, const optional<T>&) noexcept;
    template <class T>
    constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept;
    template <class T>
    constexpr bool operator>=(nullopt_t, const optional<T>& rhs) noexcept;
    
    template <class T, class U>
    constexpr bool operator==(const optional<T>& lhs, const U& rhs);
    template <class T, class U>
    constexpr bool operator==(const U& lhs, const optional<T>& rhs);
    template <class T, class U>
    constexpr bool operator!=(const optional<T>& lhs, const U& rhs);
    template <class T, class U>
    constexpr bool operator!=(const U& lhs, const optional<T>& rhs);
    template <class T, class U>
    constexpr bool operator<(const optional<T>& lhs, const U& rhs);
    template <class T, class U>
    constexpr bool operator<(const U& lhs, const optional<T>& rhs);
    template <class T, class U>
    constexpr bool operator<=(const optional<T>& lhs, const U& rhs);
    template <class T, class U>
    constexpr bool operator<=(const U& lhs, const optional<T>& rhs);
    template <class T, class U>
    constexpr bool operator>(const optional<T>& lhs, const U& rhs);
    template <class T, class U>
    constexpr bool operator>(const U& lhs, const optional<T>& rhs);
    template <class T, class U>
    constexpr bool operator>=(const optional<T>& lhs, const U& rhs);
    template <class T, class U>
    constexpr bool operator>=(const U& lhs, const optional<T>& rhs);
    
    template <class T>
    void swap(optional<T> &lhs, optional<T> &rhs);
    
    namespace detail
    {
        struct i_am_secret;
    }
    
    template <class T = detail::i_am_secret, class U, class Ret = detail::conditional_t<std::is_same<T, detail::i_am_secret>::value, detail::decay_t<U>, T>>
    constexpr optional<Ret> make_optional(U&& v);
    
    template <class T, class ... Args>
    constexpr optional<T> make_optional(Args&&... args);
    
    template <class T, class U, class ... Args>
    constexpr optional<T> make_optional(std::initializer_list<U> il, Args&&... args);
    
    template <class T>
    class optional<T&>;
}

namespace std
{
}

Class tl::monostate

class monostate
{
};

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

Struct tl::in_place_t

struct in_place_t
{
    in_place_t() = default;
};

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

Variable tl::in_place

constexpr in_place_t{} in_place;

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

Struct tl::nullopt_t

struct nullopt_t
{
    struct do_not_use;
    
    constexpr nullopt_t(do_not_use, do_not_use) noexcept;
};

A tag type to represent an empty optional

Variable tl::nullopt

static constexpr nullopt_t 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

template <class T>
class optional
{
public:
    template <class F>
    constexpr auto and_then(F &&f) &;
    template <class F>
    constexpr auto and_then(F &&f) &&;
    template <class F>
    constexpr auto and_then(F &&f) const &;
    template <class F>
    constexpr auto and_then(F &&f) const &&;
    
    template <class F> constexpr auto map(F &&f) &;
    template <class F> constexpr auto map(F &&f) &&;
    template <class F> constexpr auto map(F &&f) const&;
    template <class F> constexpr auto map(F &&f) const&&;
    
    template <class F> optional<T> or_else (F &&f) &;
    template <class F> optional<T> or_else (F &&f) &&;
    template <class F> optional<T> or_else (F &&f) const &;
    
    template <class F, class U>
    U map_or(F&& f, U&& u) &;
    template <class F, class U>
    U map_or(F&& f, U&& u) &&;
    template <class F, class U>
    U map_or(F&& f, U&& u) const &;
    template <class F, class U>
    U map_or(F&& f, U&& u) const &&;
    
    template <class F, class U>
    auto map_or_else(F &&f, U &&u) &;
    template <class F, class U>
    auto map_or_else(F &&f, U &&u)
    template <class F, class U>
    auto map_or_else(F &&f, U &&u)
    template <class F, class U>
    auto map_or_else(F &&f, U &&u)
    
    template <class U>
    constexpr optional<typename std::decay<U>::type> conjunction(U&& u) const;
    
    constexpr optional disjunction(const optional& rhs) &;
    constexpr optional disjunction(const optional& rhs) const &;
    constexpr optional disjunction(const optional& rhs) &&;
    constexpr optional disjunction(const optional& rhs) const &&;
    constexpr optional disjunction(optional&& rhs) &;
    constexpr optional disjunction(optional&& rhs) const &;
    constexpr optional disjunction(optional&& rhs) &&;
    constexpr optional disjunction(optional&& rhs) const &&;
    
    optional take() &;
    optional take() const &;
    optional take() &&;
    optional take() const &&;
    
    using value_type = T;
    
    constexpr optional() noexcept = default;
    constexpr optional(nullopt_t) noexcept;
    
    constexpr optional(const optional& rhs) = default;
    
    constexpr optional(optional&& rhs) = default;
    
    template <class... Args> constexpr explicit optional(in_place_t, Args&&... args);
    template <class U, class... Args>
    constexpr explicit optional(in_place_t, std::initializer_list<U>&, Args&&... args);
    
    template <class U=T> constexpr optional(U &&u);
    
    template <class U> optional(const optional<U> &rhs);
    
    template <class U> optional(optional<U> &&rhs);
    
    ~optional() = default;
    
    optional& operator=(nullopt_t) noexcept;
    
    optional& operator=(const optional& rhs) = default;
    
    optional& operator=(optional&& rhs) = default;
    
    optional &operator=(U &&u);
    
    optional &operator=(const optional<U> & rhs);
    
    optional &operator=(optional<U> && rhs);
    
    template <class ... Args>
    T& emplace(Args&&... args);
    template <class U, class... Args>
    T& emplace(std::initializer_list<U> il, Args &&... args);
    
    void swap(optional& rhs) noexcept(std::is_nothrow_move_constructible<T>::value&&detail::is_nothrow_swappable<T>::value);
    
    constexpr const T *operator->() const;
    constexpr T *operator->();
    
    constexpr T &operator*();
    constexpr const T &operator*() const;
    
    constexpr bool has_value() const noexcept;
    constexpr operator bool() const noexcept;
    
    constexpr T &value();
    constexpr const T &value() const;
    
    template <class U>
    constexpr T value_or(U&& u) const &;
    template <class U>
    constexpr T value_or(U&& u) &&;
    
    void reset() noexcept;
};

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

(1)  template <class F>
     constexpr auto and_then(F &&f) &;

(2)  template <class F>
     constexpr auto and_then(F &&f) &&;

(3)  template <class F>
     constexpr auto and_then(F &&f) const &;

(4)  template <class F>
     constexpr auto and_then(F &&f) const &&;

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

(1)  template <class F> constexpr auto map(F &&f) &;

(2)  template <class F> constexpr auto map(F &&f) &&;

(3)  template <class F> constexpr auto map(F &&f) const&;

(4)  template <class F> constexpr auto map(F &&f) const&&;

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

(1)  template <class F> optional<T> or_else (F &&f) &;

(2)  template <class F> optional<T> or_else (F &&f) &&;

(3)  template <class F> optional<T> or_else (F &&f) const &;

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

(1)  template <class F, class U>
     U map_or(F&& f, U&& u) &;

(2)  template <class F, class U>
     U map_or(F&& f, U&& u) &&;

(3)  template <class F, class U>
     U map_or(F&& f, U&& u) const &;

(4)  template <class F, class U>
     U map_or(F&& f, U&& u) const &&;

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

(1)  template <class F, class U>
     auto map_or_else(F &&f, U &&u) &;

(2)  template <class F, class U>
     auto map_or_else(F &&f, U &&u)

(3)  template <class F, class U>
     auto map_or_else(F &&f, U &&u)

(4)  template <class F, class U>
     auto map_or_else(F &&f, U &&u)

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

template <class U>
constexpr optional<typename std::decay<U>::type> conjunction(U&& u) const;

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

Function tl::optional::disjunction

(1)  constexpr optional disjunction(const optional& rhs) &;

(2)  constexpr optional disjunction(const optional& rhs) const &;

(3)  constexpr optional disjunction(const optional& rhs) &&;

(4)  constexpr optional disjunction(const optional& rhs) const &&;

(5)  constexpr optional disjunction(optional&& rhs) &;

(6)  constexpr optional disjunction(optional&& rhs) const &;

(7)  constexpr optional disjunction(optional&& rhs) &&;

(8)  constexpr optional disjunction(optional&& rhs) const &&;

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

Function tl::optional::take

(1)  optional take() &;

(2)  optional take() const &;

(3)  optional take() &&;

(4)  optional take() const &&;

Takes the value out of the optional, leaving it empty

Default constructor tl::optional::optional

(1)  constexpr optional() noexcept = default;

(2)  constexpr optional(nullopt_t) noexcept;

Constructs an optional that does not contain a value.

Copy constructor tl::optional::optional

constexpr optional(const optional& rhs) = default;

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

constexpr optional(optional&& rhs) = default;

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

(1)  template <class... Args> constexpr explicit optional(in_place_t, Args&&... args);

(2)  template <class U, class... Args>
     constexpr explicit optional(in_place_t, std::initializer_list<U>&, Args&&... args);

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

Function template tl::optional::optional

template <class U=T> constexpr optional(U &&u);

Constructs the stored value with u.

Function template tl::optional::optional

template <class U> optional(const optional<U> &rhs);

Converting copy constructor.

Function template tl::optional::optional

template <class U> optional(optional<U> &&rhs);

Converting move constructor.

Destructor tl::optional::~optional

~optional() = default;

Destroys the stored value if there is one.

Assignment operator tl::optional::operator=

optional& operator=(nullopt_t) noexcept;

Assignment to empty.

Destroys the current value if there is one.

Assignment operator tl::optional::operator=

optional& operator=(const optional& rhs) = default;

Copy assignment.

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

Assignment operator tl::optional::operator=

optional& operator=(optional&& rhs) = default;

Move assignment.

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

Assignment operator tl::optional::operator=

optional &operator=(U &&u);

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

Assignment operator tl::optional::operator=

optional &operator=(const optional<U> & rhs);

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=

optional &operator=(optional<U> && rhs);

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

(1)  template <class ... Args>
     T& emplace(Args&&... args);

(2)  template <class U, class... Args>
     T& emplace(std::initializer_list<U> il, Args &&... args);

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

Function tl::optional::swap

void swap(optional& rhs) noexcept(std::is_nothrow_move_constructible<T>::value&&detail::is_nothrow_swappable<T>::value);

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

(1)  constexpr const T *operator->() const;

(2)  constexpr T *operator->();

Returns: a pointer to the stored value

Requires: a value is stored

Operator tl::optional::operator*

(1)  constexpr T &operator*();

(2)  constexpr const T &operator*() const;

Returns: the stored value

Requires: a value is stored

Function tl::optional::has_value

(1)  constexpr bool has_value() const noexcept;

(2)  constexpr operator bool() const noexcept;

Returns: whether or not the optional has a value

Function tl::optional::value

(1)  constexpr T &value();

(2)  constexpr const T &value() const;

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

Function template tl::optional::value_or

(1)  template <class U>
     constexpr T value_or(U&& u) const &;

(2)  template <class U>
     constexpr T value_or(U&& u) &&;

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

Function tl::optional::reset

void reset() noexcept;

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


Comparison operator tl::operator==

(1)  template <class T, class U>
     constexpr bool operator==(const optional<T>& lhs, const optional<U>& rhs);

(2)  template <class T, class U>
     constexpr bool operator!=(const optional<T>& lhs, const optional<U>& rhs);

(3)  template <class T, class U>
     constexpr bool operator<(const optional<T>& lhs, const optional<U>& rhs);

(4)  template <class T, class U>
     constexpr bool operator>(const optional<T>& lhs, const optional<U>& rhs);

(5)  template <class T, class U>
     constexpr bool operator<=(const optional<T>& lhs, const optional<U>& rhs);

(6)  template <class T, class U>
     constexpr bool operator>=(const optional<T>& lhs, const optional<U>& rhs);

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==

(1)  template <class T>
     constexpr bool operator==(const optional<T>& lhs, nullopt_t) noexcept;

(2)  template <class T>
     constexpr bool operator==(nullopt_t, const optional<T>& rhs) noexcept;

(3)  template <class T>
     constexpr bool operator!=(const optional<T>& lhs, nullopt_t) noexcept;

(4)  template <class T>
     constexpr bool operator!=(nullopt_t, const optional<T>& rhs) noexcept;

(5)  template <class T>
     constexpr bool operator<(const optional<T>&, nullopt_t) noexcept;

(6)  template <class T>
     constexpr bool operator<(nullopt_t, const optional<T>& rhs) noexcept;

(7)  template <class T>
     constexpr bool operator<=(const optional<T>& lhs, nullopt_t) noexcept;

(8)  template <class T>
     constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept;

(9)  template <class T>
     constexpr bool operator>(const optional<T>& lhs, nullopt_t) noexcept;

(10)  template <class T>
     constexpr bool operator>(nullopt_t, const optional<T>&) noexcept;

(11)  template <class T>
     constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept;

(12)  template <class T>
     constexpr bool operator>=(nullopt_t, const optional<T>& rhs) noexcept;

Compares an optional to a nullopt

Equivalent to comparing the optional to an empty optional

Comparison operator tl::operator==

(1)  template <class T, class U>
     constexpr bool operator==(const optional<T>& lhs, const U& rhs);

(2)  template <class T, class U>
     constexpr bool operator==(const U& lhs, const optional<T>& rhs);

(3)  template <class T, class U>
     constexpr bool operator!=(const optional<T>& lhs, const U& rhs);

(4)  template <class T, class U>
     constexpr bool operator!=(const U& lhs, const optional<T>& rhs);

(5)  template <class T, class U>
     constexpr bool operator<(const optional<T>& lhs, const U& rhs);

(6)  template <class T, class U>
     constexpr bool operator<(const U& lhs, const optional<T>& rhs);

(7)  template <class T, class U>
     constexpr bool operator<=(const optional<T>& lhs, const U& rhs);

(8)  template <class T, class U>
     constexpr bool operator<=(const U& lhs, const optional<T>& rhs);

(9)  template <class T, class U>
     constexpr bool operator>(const optional<T>& lhs, const U& rhs);

(10)  template <class T, class U>
     constexpr bool operator>(const U& lhs, const optional<T>& rhs);

(11)  template <class T, class U>
     constexpr bool operator>=(const optional<T>& lhs, const U& rhs);

(12)  template <class T, class U>
     constexpr bool operator>=(const U& lhs, const optional<T>& rhs);

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&>

template <class T>
class optional<T&>
{
public:
    template <class F>
    constexpr auto and_then(F &&f) &;
    template <class F>
    constexpr auto and_then(F &&f) &&;
    template <class F>
    constexpr auto and_then(F &&f) const &;
    template <class F>
    constexpr auto and_then(F &&f) const &&;
    
    template <class F> constexpr auto map(F &&f) &;
    template <class F> constexpr auto map(F &&f) &&;
    template <class F> constexpr auto map(F &&f) const&;
    template <class F> constexpr auto map(F &&f) const&&;
    
    template <class F> optional<T> or_else (F &&f) &;
    template <class F> optional<T> or_else (F &&f) &&;
    template <class F> optional<T> or_else (F &&f) const &;
    
    template <class F, class U>
    U map_or(F&& f, U&& u) &;
    template <class F, class U>
    U map_or(F&& f, U&& u) &&;
    template <class F, class U>
    U map_or(F&& f, U&& u) const &;
    template <class F, class U>
    U map_or(F&& f, U&& u) const &&;
    
    template <class F, class U>
    auto map_or_else(F &&f, U &&u) &;
    template <class F, class U>
    auto map_or_else(F &&f, U &&u)
    template <class F, class U>
    auto map_or_else(F &&f, U &&u)
    template <class F, class U>
    auto map_or_else(F &&f, U &&u)
    
    template <class U>
    constexpr optional<typename std::decay<U>::type> conjunction(U&& u) const;
    
    constexpr optional disjunction(const optional& rhs) &;
    constexpr optional disjunction(const optional& rhs) const &;
    constexpr optional disjunction(const optional& rhs) &&;
    constexpr optional disjunction(const optional& rhs) const &&;
    constexpr optional disjunction(optional&& rhs) &;
    constexpr optional disjunction(optional&& rhs) const &;
    constexpr optional disjunction(optional&& rhs) &&;
    constexpr optional disjunction(optional&& rhs) const &&;
    
    optional take() &;
    optional take() const &;
    optional take() &&;
    optional take() const &&;
    
    using value_type = T&;
    
    constexpr optional() noexcept;
    constexpr optional(nullopt_t) noexcept;
    
    constexpr optional(const optional& rhs) noexcept = default;
    
    constexpr optional(optional&& rhs) = default;
    
    template <class U=T> constexpr optional(U &&u);
    
    ~optional() = default;
    
    optional& operator=(nullopt_t) noexcept;
    
    optional& operator=(const optional& rhs) = default;
    
    optional &operator=(U &&u);
    
    template <class U>
    optional& operator=(const optional<U>& rhs);
    
    template <class ... Args>
    T& emplace(Args&&... args) noexcept;
    
    void swap(optional& rhs) noexcept;
    
    constexpr const T *operator->() const;
    constexpr T *operator->();
    
    constexpr T &operator*();
    constexpr const T &operator*() const;
    
    constexpr bool has_value() const noexcept;
    constexpr operator bool() const noexcept;
    
    constexpr T& value() &;
    constexpr const T &value() const;
    
    template <class U>
    constexpr T value_or(U&& u) const &;
    template <class U>
    constexpr T value_or(U&& u) &&;
    
    void reset() noexcept;
};

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

(1)  template <class F>
     constexpr auto and_then(F &&f) &;

(2)  template <class F>
     constexpr auto and_then(F &&f) &&;

(3)  template <class F>
     constexpr auto and_then(F &&f) const &;

(4)  template <class F>
     constexpr auto and_then(F &&f) const &&;

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

(1)  template <class F> constexpr auto map(F &&f) &;

(2)  template <class F> constexpr auto map(F &&f) &&;

(3)  template <class F> constexpr auto map(F &&f) const&;

(4)  template <class F> constexpr auto map(F &&f) const&&;

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

(1)  template <class F> optional<T> or_else (F &&f) &;

(2)  template <class F> optional<T> or_else (F &&f) &&;

(3)  template <class F> optional<T> or_else (F &&f) const &;

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

(1)  template <class F, class U>
     U map_or(F&& f, U&& u) &;

(2)  template <class F, class U>
     U map_or(F&& f, U&& u) &&;

(3)  template <class F, class U>
     U map_or(F&& f, U&& u) const &;

(4)  template <class F, class U>
     U map_or(F&& f, U&& u) const &&;

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

(1)  template <class F, class U>
     auto map_or_else(F &&f, U &&u) &;

(2)  template <class F, class U>
     auto map_or_else(F &&f, U &&u)

(3)  template <class F, class U>
     auto map_or_else(F &&f, U &&u)

(4)  template <class F, class U>
     auto map_or_else(F &&f, U &&u)

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

template <class U>
constexpr optional<typename std::decay<U>::type> conjunction(U&& u) const;

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

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

(1)  constexpr optional disjunction(const optional& rhs) &;

(2)  constexpr optional disjunction(const optional& rhs) const &;

(3)  constexpr optional disjunction(const optional& rhs) &&;

(4)  constexpr optional disjunction(const optional& rhs) const &&;

(5)  constexpr optional disjunction(optional&& rhs) &;

(6)  constexpr optional disjunction(optional&& rhs) const &;

(7)  constexpr optional disjunction(optional&& rhs) &&;

(8)  constexpr optional disjunction(optional&& rhs) const &&;

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

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

(1)  optional take() &;

(2)  optional take() const &;

(3)  optional take() &&;

(4)  optional take() const &&;

Takes the value out of the optional, leaving it empty

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

(1)  constexpr optional() noexcept;

(2)  constexpr optional(nullopt_t) noexcept;

Constructs an optional that does not contain a value.

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

constexpr optional(const optional& rhs) noexcept = default;

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

constexpr optional(optional&& rhs) = default;

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

template <class U=T> constexpr optional(U &&u);

Constructs the stored value with u.

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

~optional() = default;

No-op

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

optional& operator=(nullopt_t) noexcept;

Assignment to empty.

Destroys the current value if there is one.

Copy assignment operator tl::optional<T&>::operator=

optional& operator=(const optional& rhs) = default;

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=

optional &operator=(U &&u);

Rebinds this optional to u.

Requires: U must be an lvalue reference.

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

template <class U>
optional& operator=(const optional<U>& rhs);

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

(1)  template <class ... Args>
     T& emplace(Args&&... args) noexcept;

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

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

void swap(optional& rhs) noexcept;

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

(1)  constexpr const T *operator->() const;

(2)  constexpr T *operator->();

Returns: a pointer to the stored value

Requires: a value is stored

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

(1)  constexpr T &operator*();

(2)  constexpr const T &operator*() const;

Returns: the stored value

Requires: a value is stored

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

(1)  constexpr bool has_value() const noexcept;

(2)  constexpr operator bool() const noexcept;

Returns: whether or not the optional has a value

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

(1)  constexpr T& value() &;

(2)  constexpr const T &value() const;

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

synopsis constexpr T \&value();

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

(1)  template <class U>
     constexpr T value_or(U&& u) const &;

(2)  template <class U>
     constexpr T value_or(U&& u) &&;

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

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

void reset() noexcept;

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