sumty  0.1.0
Better sum types for C++
option< T > Class Template Reference

Type that either contains some value or none. More...

#include <sumty/option.hpp>

Inheritance diagram for option< T >:
Collaboration diagram for option< T >:

Public Types

using value_type = ...
 
using reference = ...
 
using const_reference = ...
 
using rvalue_reference = ...
 
using const_rvalue_reference = ...
 
using pointer = ...
 
using const_pointer = ...
 

Public Member Functions

constexpr option () noexcept
 Default constructor. More...
 
constexpr option (const option &) CONDITIONALLY_NOEXCEPT
 Copy constructor. More...
 
constexpr option (option &&) CONDITIONALLY_NOEXCEPT
 Move constructor. More...
 
constexpr option ([[maybe_unused]] none_t null) noexcept
 None constructor. More...
 
constexpr option ([[maybe_unused]] std::nullopt_t null) noexcept
 std::nullopt constructor More...
 
constexpr option ([[maybe_unused]] std::nullptr_t null) noexcept
 nullptr constructor More...
 
template<typename U >
constexpr CONDITIONALLY_EXPLICIT option (U *ptr) noexcept
 Pointer constructor. More...
 
template<typename U >
constexpr CONDITIONALLY_EXPLICIT option (const option< U > &other)
 Converting copy constructor. More...
 
template<typename U >
constexpr CONDITIONALLY_EXPLICIT option (option< U > &&other)
 Converting move constructor. More...
 
template<typename... Args>
constexpr CONDITIONALLY_EXPLICIT option ([[maybe_unused]] std::in_place_t inplace, Args &&... args)
 Emplacement constructor. More...
 
template<typename U , typename... Args>
constexpr option ([[maybe_unused]] std::in_place_t inplace, std::initializer_list< U > init, Args &&... args)
 Emplacement constructor with initializer list. More...
 
template<typename U >
constexpr CONDITIONALLY_EXPLICIT option (U &&value)
 Forwarding constructor. More...
 
constexpr ~option () CONDITIONALLY_NOEXCEPT
 Destructor. More...
 
constexpr optionoperator= (const option &) CONDITIONALLY_NOEXCEPT
 Copy assignment operator. More...
 
constexpr optionoperator= (option &&) CONDITIONALLY_NOEXCEPT
 Move assignment operator. More...
 
constexpr optionoperator= ([[maybe_unused]] none_t null) CONDTIONALLY_NOEXCEPT
 none assignment operator More...
 
constexpr optionoperator= ([[maybe_unused]] std::nullopt_t null) CONDITIONALLY_NOEXCEPT
 std::nullopt assignment operator More...
 
constexpr optionoperator= ([[maybe_unused]] std::nullptr_t null) noexcept
 nullptr assignment operator More...
 
template<typename U >
constexpr optionoperator= (U &&value)
 Value assignment operator. More...
 
template<typename U >
constexpr optionoperator= (U *ptr) noexcept
 Pointer assignment operator. More...
 
template<typename U >
constexpr optionoperator= (const option< U > &value)
 Converting copy assignment operator. More...
 
template<typename U >
constexpr optionoperator= (option< U > &&value)
 Converting move assignment operator. More...
 
constexpr operator bool () const noexcept
 Implicit conversion to bool. More...
 
template<typename U >
constexpr CONDITIONALLY_EXPLICIT operator U* () const noexcept
 Conversion to raw pointer. More...
 
constexpr bool has_value () const noexcept
 Returns true if the option contains a value. More...
 
constexpr bool is_some () const noexcept
 Returns true if the option contains a value. More...
 
constexpr bool is_none () const noexcept
 Returns true if the options does not contain a value. More...
 
constexpr reference operator* () &noexcept
 Accesses the value contained in the option. More...
 
constexpr const_reference operator* () const &noexcept
 Accesses the value contained in the option. More...
 
constexpr rvalue_reference operator* () &&
 Accesses the value contained in the option. More...
 
constexpr const_rvalue_reference operator* () const &&
 Accesses the value contained in the option. More...
 
constexpr pointer operator-> () noexcept
 Accesses members of the value contained in the option. More...
 
constexpr const_pointer operator-> () const noexcept
 Accesses members of the value contained in the option. More...
 
constexpr reference value () &
 Accesses the value contained in the option. More...
 
constexpr const_reference value () const &
 Accesses the value contained in the option. More...
 
constexpr rvalue_reference value () &&
 Accesses the value contained in the option. More...
 
constexpr rvalue_reference value () const &&
 Accesses the value contained in the option. More...
 
template<typename U >
constexpr value_type value_or (U &&default_value) const &
 Gets the option value with a default used for none. More...
 
template<typename U >
constexpr value_type value_or (U &&default_value) &&
 Gets the option value with a default used for none. More...
 
template<typename F >
constexpr value_type value_or_else (F &&f) const &
 Gets the option value with a default used for none. More...
 
template<typename F >
constexpr value_type value_or_else (F &&f) &&
 Gets the option value with a default used for none. More...
 
constexpr reference some () &
 Accesses the value contained in the option. More...
 
constexpr const_reference some () const &
 Accesses the value contained in the option. More...
 
constexpr rvalue_reference some () &&
 Accesses the value contained in the option. More...
 
constexpr rvalue_reference some () const &&
 Accesses the value contained in the option. More...
 
template<typename U >
constexpr value_type some_or (U &&default_value) const &
 Gets the option value with a default used for none. More...
 
template<typename U >
constexpr value_type some_or (U &&default_value) &&
 Gets the option value with a default used for none. More...
 
template<typename F >
constexpr value_type some_or_else (F &&f) const &
 Gets the option value with a default used for none. More...
 
template<typename F >
constexpr value_type some_or_else (F &&f) &&
 Gets the option value with a default used for none. More...
 
template<typename E >
constexpr result< T, std::remove_cvref_t< E > > ok_or (E &&err) const &
 Converts an option to a result. More...
 
template<typename E >
constexpr result< T, std::remove_cvref_t< E > > ok_or (E &&err) &&
 Converts an option to a result. More...
 
template<typename E >
constexpr result< T, E > ok_or (const result< never, E > &err) const &
 Converts an option to a result. More...
 
template<typename E >
constexpr result< T, E > ok_or (const result< never, E > &err) &&
 Converts an option to a result. More...
 
template<typename E >
constexpr result< T, E > ok_or (result< never, E > &&err) const &
 Converts an option to a result. More...
 
template<typename E >
constexpr result< T, E > ok_or (result< never, E > &&err) &&
 Converts an option to a result. More...
 
template<typename F >
constexpr result< T, std::invoke_result_t< F > > ok_or_else (F &&f) const &
 Converts an option to a result. More...
 
template<typename F >
constexpr result< T, std::invoke_result_t< F > > ok_or_else (F &&f) &&
 Converts an option to a result. More...
 
template<typename U >
constexpr result< std::remove_cvref_t< U >, T > error_or (U &&value) const &
 Converts an option to a result. More...
 
template<typename U >
constexpr result< std::remove_cvref_t< U >, T > error_or (U &&value) &&
 Converts an option to a result. More...
 
template<typename U >
constexpr result< U, T > error_or (const result< U, never > &value) const &
 Converts an option to a result. More...
 
template<typename U >
constexpr result< U, T > error_or (const result< U, never > &value) &&
 Converts an option to a result. More...
 
template<typename U >
constexpr result< U, T > error_or (result< U, never > &&value) const &
 Converts an option to a result. More...
 
template<typename U >
constexpr result< U, T > error_or (result< U, never > &&value) &&
 Converts an option to a result. More...
 
template<typename F >
constexpr result< std::invoke_result_t< F >, T > error_or_else (F &&f) const &
 Converts an option to a result. More...
 
template<typename F >
constexpr result< std::invoke_result_t< F >, T > error_or_else (F &&f) &&
 Converts an option to a result. More...
 
constexpr option< reference > ref () noexcept
 Converts an option into an option of a reference. More...
 
constexpr option< const_reference > ref () const noexcept
 Converts an option into an option of a reference. More...
 
constexpr option< const_reference > cref () const noexcept
 Converts an option into an option of a reference. More...
 
template<typename F >
constexpr auto and_then (F &&f) &
 Applies a callable to the contents of an option. More...
 
template<typename F >
constexpr auto and_then (F &&f) const &
 Applies a callable to the contents of an option. More...
 
template<typename F >
constexpr auto and_then (F &&f) &&
 Applies a callable to the contents of an option. More...
 
template<typename F >
constexpr auto and_then (F &&f) const &&
 Applies a callable to the contents of an option. More...
 
template<typename F >
constexpr auto transform (F &&f) &
 Performs a transformation on the content of an option. More...
 
template<typename F >
constexpr auto transform (F &&f) const &
 Performs a transformation on the content of an option. More...
 
template<typename F >
constexpr auto transform (F &&f) &&
 Performs a transformation on the content of an option. More...
 
template<typename F >
constexpr auto transform (F &&f) const &&
 Performs a transformation on the content of an option. More...
 
template<typename F >
constexpr auto map (F &&f) &
 Performs a transformation on the content of an option. More...
 
template<typename F >
constexpr auto map (F &&f) const &
 Performs a transformation on the content of an option. More...
 
template<typename F >
constexpr auto map (F &&f) &&
 Performs a transformation on the content of an option. More...
 
template<typename F >
constexpr auto map (F &&f) const &&
 Performs a transformation on the content of an option. More...
 
template<typename F >
constexpr option or_else (F &&f) const &
 Returns the result of f if the option is none. More...
 
template<typename F >
constexpr option or_else (F &&f) &&
 Returns the result of f if the option is none. More...
 
constexpr T flatten () const &
 Converts option<option<T>> to option<T> More...
 
constexpr T flatten () &&
 Converts option<option<T>> to option<T> More...
 
constexpr auto flatten_all () const &
 Converts nested option types into a single option. More...
 
constexpr auto flatten_all () &&
 Converts nested option types into a single option. More...
 
template<typename Pred >
constexpr option filter (Pred &&pred) const &
 Filters the option based on a predicate. More...
 
template<typename Pred >
constexpr option filter (Pred &&pred) &&
 Filters the option based on a predicate. More...
 
constexpr auto transpose () const &
 Converts option<result<T, E>> into result<option<T>, E> More...
 
constexpr auto transpose () &&
 Converts option<result<T, E>> into result<option<T>, E> More...
 
template<typename V >
constexpr DEDUCED visit (V &&visitor) &
 Calls a visitor callable with the contained value or void. More...
 
template<typename V >
constexpr DEDUCED visit (V &&visitor) const &
 Calls a visitor callable with the contained value or void. More...
 
template<typename V >
constexpr DEDUCED visit (V &&visitor) &&
 Calls a visitor callable with the contained value or void. More...
 
template<typename V >
constexpr DEDUCED visit (V &&visitor) const &&
 Calls a visitor callable with the contained value or void. More...
 
template<typename V >
constexpr DEDUCED visit_informed (V &&visitor) &
 Calls a visitor callable with the contained value and meta data. More...
 
template<typename V >
constexpr DEDUCED visit_informed (V &&visitor) const &
 Calls a visitor callable with the contained value and meta data. More...
 
template<typename V >
constexpr DEDUCED visit_informed (V &&visitor) &&
 Calls a visitor callable with the contained value and meta data. More...
 
template<typename V >
constexpr DEDUCED visit_informed (V &&visitor) const &&
 Calls a visitor callable with the contained value and meta data. More...
 
constexpr void swap (option &other) CONDITIONALLY_NOEXCEPT
 Swaps two option instances. More...
 
constexpr void reset () noexcept
 Sets the option to none. More...
 
template<typename... Args>
constexpr reference emplace (Args &&... args)
 Constructs a new value in place into the option. More...
 

Related Functions

(Note that these are not member functions.)

template<size_t IDX, typename T >
constexpr REFERENCE get (option< T > &opt)
 Gets an option value by index, as if it were a variant. More...
 
template<size_t IDX, typename T >
constexpr CONST_REFERENCE get (const option< T > &opt)
 Gets an option value by index, as if it were a variant. More...
 
template<size_t IDX, typename T >
constexpr RVALUE_REFERENCE get (option< T > &&opt)
 Gets an option value by index, as if it were a variant. More...
 
template<size_t IDX, typename T >
constexpr CONST_RVALUE_REFERENCE get (const option< T > &&opt)
 Gets an option value by index, as if it were a variant. More...
 
template<typename T , typename U >
constexpr REFERENCE get (option< U > &opt)
 Gets an option value by type, as if it were a variant. More...
 
template<typename T , typename U >
constexpr CONST_REFERENCE get (const option< U > &opt)
 Gets an option value by type, as if it were a variant. More...
 
template<typename T , typename U >
constexpr RVALUE_REFERENCE get (option< U > &&opt)
 Gets an option value by type, as if it were a variant. More...
 
template<typename T , typename U >
constexpr CONST_RVALUE_REFERENCE get (const option< U > &&opt)
 Gets an option value by type, as if it were a variant. More...
 
template<typename T , typename U >
constexpr bool operator== (const option< T > &lhs, const option< U > &rhs)
 Tests two option instances for equality. More...
 
template<typename T , typename U >
constexpr bool operator!= (const option< T > &lhs, const option< U > &rhs)
 Tests two option instances for inequality. More...
 
template<typename T , typename U >
constexpr bool operator< (const option< T > &lhs, const option< U > &rhs)
 Compares two option instances. More...
 
template<typename T , typename U >
constexpr bool operator> (const option< T > &lhs, const option< U > &rhs)
 Compares two option instances. More...
 
template<typename T , typename U >
constexpr bool operator<= (const option< T > &lhs, const option< U > &rhs)
 Compares two option instances. More...
 
template<typename T , typename U >
constexpr bool operator>= (const option< T > &lhs, const option< U > &rhs)
 Compares two option instances. More...
 
template<typename T , typename U >
constexpr auto operator (const option< T > &lhs, const option< U > &rhs)
 Compares two option instances. More...
 
template<typename T , typename U >
constexpr bool operator== (const option< T > &lhs, const U &rhs)
 Compares an option with a value. More...
 
template<typename T , typename U >
constexpr bool operator== (const U &lhs, const option< T > &rhs)
 Compares an option with a value. More...
 
template<typename T , typename U >
constexpr bool operator!= (const option< T > &lhs, const U &rhs)
 Compares an option with a value. More...
 
template<typename T , typename U >
constexpr bool operator!= (const U &lhs, const option< T > &rhs)
 Compares an option with a value. More...
 
template<typename T , typename U >
constexpr bool operator< (const option< T > &lhs, const U &rhs)
 Compares an option with a value. More...
 
template<typename T , typename U >
constexpr bool operator< (const U &lhs, const option< T > &rhs)
 Compares an option with a value. More...
 
template<typename T , typename U >
constexpr bool operator> (const option< T > &lhs, const U &rhs)
 Compares an option with a value. More...
 
template<typename T , typename U >
constexpr bool operator> (const U &lhs, const option< T > &rhs)
 Compares an option with a value. More...
 
template<typename T , typename U >
constexpr bool operator<= (const option< T > &lhs, const U &rhs)
 Compares an option with a value. More...
 
template<typename T , typename U >
constexpr bool operator<= (const U &lhs, const option< T > &rhs)
 Compares an option with a value. More...
 
template<typename T , typename U >
constexpr bool operator>= (const option< T > &lhs, const U &rhs)
 Compares an option with a value. More...
 
template<typename T , typename U >
constexpr bool operator>= (const U &lhs, const option< T > &rhs)
 Compares an option with a value. More...
 
template<typename T , typename U >
constexpr auto operator (const option< T > &lhs, const U &rhs)
 Compares an option with a value. More...
 
template<typename T >
constexpr bool operator== (const option< T > &lhs,[[maybe_unused]] none_t rhs)
 Compares an option with none. More...
 
template<typename T >
constexpr bool operator== ([[maybe_unused]] none_t lhs, const option< T > &rhs)
 Compares an option with none. More...
 
template<typename T >
constexpr bool operator!= (const option< T > &lhs,[[maybe_unused]] none_t rhs)
 Compares an option with none. More...
 
template<typename T >
constexpr bool operator!= ([[maybe_unused]] none_t lhs, const option< T > &rhs)
 Compares an option with none. More...
 
template<typename T >
constexpr bool operator< ([[maybe_unused]] const option< T > &lhs,[[maybe_unused]] none_t rhs)
 Compares an option with none. More...
 
template<typename T >
constexpr bool operator< ([[maybe_unused]] none_t lhs, const option< T > &rhs)
 Compares an option with none. More...
 
template<typename T >
constexpr bool operator> (const option< T > &lhs,[[maybe_unused]] none_t rhs)
 Compares an option with none. More...
 
template<typename T >
constexpr bool operator> ([[maybe_unused]] none_t lhs,[[maybe_unused]] const option< T > &rhs)
 Compares an option with none. More...
 
template<typename T >
constexpr bool operator<= (const option< T > &lhs,[[maybe_unused]] none_t rhs)
 Compares an option with none. More...
 
template<typename T >
constexpr bool operator<= ([[maybe_unused]] none_t lhs,[[maybe_unused]] const option< T > &rhs)
 Compares an option with none. More...
 
template<typename T >
constexpr bool operator>= ([[maybe_unused]] const option< T > &lhs,[[maybe_unused]] none_t rhs)
 Compares an option with none. More...
 
template<typename T >
constexpr bool operator>= ([[maybe_unused]] none_t lhs, const option< T > &rhs)
 Compares an option with none. More...
 
template<typename T >
constexpr std::strong_ordering operator (const option< T > &lhs,[[maybe_unused]] none_t rhs)
 Compares an option with none. More...
 
template<typename T >
constexpr bool operator== (const option< T > &lhs,[[maybe_unused]] std::nullopt_t rhs)
 Compares an option with std::nullopt. More...
 
template<typename T >
constexpr bool operator== ([[maybe_unused]] std::nullopt_t lhs, const option< T > &rhs)
 Compares an option with std::nullopt. More...
 
template<typename T >
constexpr bool operator!= (const option< T > &lhs,[[maybe_unused]] std::nullopt_t rhs)
 Compares an option with std::nullopt. More...
 
template<typename T >
constexpr bool operator!= ([[maybe_unused]] std::nullopt_t lhs, const option< T > &rhs)
 Compares an option with std::nullopt. More...
 
template<typename T >
constexpr bool operator< ([[maybe_unused]] const option< T > &lhs,[[maybe_unused]] std::nullopt_t rhs)
 Compares an option with std::nullopt. More...
 
template<typename T >
constexpr bool operator< ([[maybe_unused]] std::nullopt_t lhs, const option< T > &rhs)
 Compares an option with std::nullopt. More...
 
template<typename T >
constexpr bool operator> (const option< T > &lhs,[[maybe_unused]] std::nullopt_t rhs)
 Compares an option with std::nullopt. More...
 
template<typename T >
constexpr bool operator> ([[maybe_unused]] std::nullopt_t lhs,[[maybe_unused]] const option< T > &rhs)
 Compares an option with std::nullopt. More...
 
template<typename T >
constexpr bool operator<= (const option< T > &lhs,[[maybe_unused]] std::nullopt_t rhs)
 Compares an option with std::nullopt. More...
 
template<typename T >
constexpr bool operator<= ([[maybe_unused]] std::nullopt_t lhs,[[maybe_unused]] const option< T > &rhs)
 Compares an option with std::nullopt. More...
 
template<typename T >
constexpr bool operator>= ([[maybe_unused]] const option< T > &lhs,[[maybe_unused]] std::nullopt_t rhs)
 Compares an option with std::nullopt. More...
 
template<typename T >
constexpr bool operator>= ([[maybe_unused]] std::nullopt_t lhs, const option< T > &rhs)
 Compares an option with std::nullopt. More...
 
template<typename T >
constexpr std::strong_ordering operator (const option< T > &lhs,[[maybe_unused]] std::nullopt_t rhs)
 Compares an option with std::nullopt. More...
 
template<typename T >
constexpr bool operator== (const option< T > &lhs,[[maybe_unused]] std::nullptr_t rhs)
 Compares an option with nullptr. More...
 
template<typename T >
constexpr bool operator== ([[maybe_unused]] std::nullptr_t lhs, const option< T > &rhs)
 Compares an option with nullptr. More...
 
template<typename T >
constexpr bool operator!= (const option< T > &lhs,[[maybe_unused]] std::nullptr_t rhs)
 Compares an option with nullptr. More...
 
template<typename T >
constexpr bool operator!= ([[maybe_unused]] std::nullptr_t lhs, const option< T > &rhs)
 Compares an option with nullptr. More...
 
template<typename T >
constexpr bool operator< ([[maybe_unused]] const option< T > &lhs,[[maybe_unused]] std::nullptr_t rhs)
 Compares an option with nullptr. More...
 
template<typename T >
constexpr bool operator< ([[maybe_unused]] std::nullptr_t lhs, const option< T > &rhs)
 Compares an option with nullptr. More...
 
template<typename T >
constexpr bool operator> (const option< T > &lhs,[[maybe_unused]] std::nullptr_t rhs)
 Compares an option with nullptr. More...
 
template<typename T >
constexpr bool operator> ([[maybe_unused]] std::nullptr_t lhs,[[maybe_unused]] const option< T > &rhs)
 Compares an option with nullptr. More...
 
template<typename T >
constexpr bool operator<= (const option< T > &lhs,[[maybe_unused]] std::nullptr_t rhs)
 Compares an option with nullptr. More...
 
template<typename T >
constexpr bool operator<= ([[maybe_unused]] std::nullptr_t lhs,[[maybe_unused]] const option< T > &rhs)
 Compares an option with nullptr. More...
 
template<typename T >
constexpr bool operator>= ([[maybe_unused]] const option< T > &lhs,[[maybe_unused]] std::nullptr_t rhs)
 Compares an option with nullptr. More...
 
template<typename T >
constexpr bool operator>= ([[maybe_unused]] std::nullptr_t lhs, const option< T > &rhs)
 Compares an option with nullptr. More...
 
template<typename T >
constexpr std::strong_ordering operator (const option< T > &lhs,[[maybe_unused]] std::nullptr_t rhs)
 Compares an option with nullptr. More...
 
template<typename T , typename U >
constexpr bool operator== (const option< T > &lhs, const U *rhs)
 Compares an option with a pointer. More...
 
template<typename T , typename U >
constexpr bool operator!= (const option< T > &lhs, const U *rhs)
 Compares an option with a pointer. More...
 
template<typename T , typename U >
constexpr bool operator< (const option< T > &lhs, const U *rhs)
 Compares an option with a pointer. More...
 
template<typename T , typename U >
constexpr bool operator> (const option< T > &lhs, const U *rhs)
 Compares an option with a pointer. More...
 
template<typename T , typename U >
constexpr bool operator<= (const option< T > &lhs, const U *rhs)
 Compares an option with a pointer. More...
 
template<typename T , typename U >
constexpr bool operator>= (const option< T > &lhs, const U *rhs)
 Compares an option with a pointer. More...
 
template<typename T , typename U >
constexpr bool operator== (const U *lhs, const option< T > &rhs)
 Compares an option with a pointer. More...
 
template<typename T , typename U >
constexpr bool operator!= (const U *lhs, const option< T > &rhs)
 Compares an option with a pointer. More...
 
template<typename T , typename U >
constexpr bool operator< (const U *lhs, const option< T > &rhs)
 Compares an option with a pointer. More...
 
template<typename T , typename U >
constexpr bool operator> (const U *lhs, const option< T > &rhs)
 Compares an option with a pointer. More...
 
template<typename T , typename U >
constexpr bool operator<= (const U *lhs, const option< T > &rhs)
 Compares an option with a pointer. More...
 
template<typename T , typename U >
constexpr bool operator>= (const U *lhs, const option< T > &rhs)
 Compares an option with a pointer. More...
 
template<typename T , typename U >
constexpr auto operator (const option< T > &lhs, const U *rhs)
 Compares an option with a pointer. More...
 
template<typename T , typename... Args>
constexpr option< T > some (Args &&... args)
 Constructs an option that contains a value. More...
 
template<typename T , typename U , typename... Args>
constexpr option< T > some (std::initializer_list< U > ilist, Args &&... args)
 Constructs an option that contains a value. More...
 
template<typename T >
constexpr void swap (option< T > &a, option< T > &b) CONDITIONALLY_NOEXCEPT
 Swaps two option instances. More...
 

Detailed Description

template<typename T>
class sumty::option< T >

Type that either contains some value or none.

option is a reimplementation of std::optional with several improvements. The key difference is that option can contain void and references (lvalue and rvalue). option<void> turns out to essentially be a bool with the option interface, and option<T&> behaves like non-owning, nullable smart pointer to T.

Internally, option<T> is represented as a variant<void, T>. Thus, option benefits from the size optimizations implemented by variant (see variant documentation for details). In particular, an option of void or an empty type has the size of a bool, and an option of an lvalue reference has the size of a raw pointer.

struct empty_t {};
assert(sizeof(option<void>) == sizeof(bool));
assert(sizeof(option<empty_t>) == sizeof(bool));
assert(sizeof(option<int&>) == sizeof(int*));
assert(sizeof(option<const int&>) == sizeof(const int*));

In practice, the benefit of option over std::optional is that option can be used in more places, espeicially with generic code. A generic function (function template) that wants to be able to return a value of any type, but also allow that return value to be "null" or "none" can simply return a option<T>, where T is now allowed to be a reference or even void.

// Returns the result of invoking func, even if that result is void or a
// reference, if the condition is true. Otherwise returns none.
template <typename F>
option<std::invoke_result_t<F>> call_if(bool condition, F&& func) {
if (condition) {
// sumty::invoke needed to handle `void` return type
return invoke(std::forward<F>(func));
} else {
return none;
}
}

An option of an lvalue reference can also be used as an alternative to raw pointers is many cases. option<T&> communicates that the pointer is allowed to be null and that the pointer is non-owning. Depending on a project's C++ coding practices, option<T&> may be preferable to T*, for single-object pointers.

Template Parameters
TThe possibly contained type

Constructor & Destructor Documentation

◆ option() [1/12]

constexpr option ( )
constexprnoexcept

Default constructor.

Initializes the option as none or null.

Example

option<int> opt;
assert(!opt);
assert(opt.has_value() == false);

◆ option() [2/12]

constexpr option ( const option< T > &  )
constexpr

Copy constructor.

If the source option is not none, the new option is initialized with a value copy constructed from the value contained in the source option.

Example

option<int> opt1;
option<int> opt2 = 42;
option<int> opt1_copy{opt1};
option<int> opt2_copy{opt2};
assert(!opt1_copy);
assert(opt2_copy);
assert(*opt2_copy == 42);

◆ option() [3/12]

constexpr option ( option< T > &&  )
constexpr

Move constructor.

If the source option is not none, the new option is initialized with a value move constructed from the value contained in the source option.

Example

option<int> opt1;
option<int> opt2;
option<int> opt1_moved{std::move(opt1)};
option<int> opt2_moved{std::move(opt2)};
assert(!opt1_moved);
assert(opt2_moved);
assert(*opt2_moved == 42);

◆ option() [4/12]

constexpr option ( [[maybe_unused] ] none_t  null)
inlineconstexprnoexcept

None constructor.

Initializes the option to the none value. That is, the option will contain no value, and .has_value() will return false.

Example

option<int> opt{none};
assert(!opt);
assert(opt.has_value() == false);
assert(opt == none);

◆ option() [5/12]

constexpr option ( [[maybe_unused] ] std::nullopt_t  null)
inlineconstexprnoexcept

std::nullopt constructor

Initializes the option to the none value. That is, the option will contain no value, and .has_value() will return false.

Example

option<int> opt{std::nullopt};
assert(!opt);
assert(opt.has_value() == false);
assert(opt == std::nullopt);

◆ option() [6/12]

constexpr option ( [[maybe_unused] ] std::nullptr_t  null)
inlineconstexprnoexcept

nullptr constructor

Initializes the option to the none value. That is, the option will contain no value, and .has_value() will return false.

This constructor only participates in overload resolution if the contained value type is an lvalue reference, in which case the option behaves like a smart pointer.

Example

option<int&> opt{nullptr};
assert(!opt);
assert(opt.has_value() == false);
assert(opt == nullptr);

◆ option() [7/12]

constexpr CONDITIONALLY_EXPLICIT option ( U *  ptr)
inlineconstexprnoexcept

Pointer constructor.

Initializes the option to contain a reference to the value that the pointer points to. If the pointer is null, the option will be none. That is, the option will contain no value, and .has_value() will return false.

This constructor only participates in overload resolution if the contained value type is an lvalue reference, in which case the option behaves like a smart pointer.

This constructor is explicit if the pointer is not implicitly convertible to T*.

Example

int value = 42;
int* ptr = &value;
int* null_ptr = nullptr;
option<int&> opt1{ptr};
option<int&> opt2{null_ptr};
assert(opt1);
assert(opt1 == ptr);
assert(!opt2);
assert(opt2 == null_ptr);
constexpr reference value() &
Accesses the value contained in the option.
Definition: option.hpp:1082

◆ option() [8/12]

constexpr CONDITIONALLY_EXPLICIT option ( const option< U > &  other)
inlineconstexpr

Converting copy constructor.

Initializes the option to contain the value of the source option where the contained value U is converted to T, as if by T{U_value}. If the source option is none, then the destination option will also be none.

This constructor only participates in overload resolution if the value type T of the destination option is constructible from the contained value of the source option U.

This constructor is explicit if U is not implicitly convertible to T.

Example

option<float> opt1{3.14};
option<int> opt2{opt1};
assert(opt2);
assert(*opt2 == 3);

◆ option() [9/12]

constexpr CONDITIONALLY_EXPLICIT option ( option< U > &&  other)
inlineconstexpr

Converting move constructor.

Initializes the option to contain the value of the source option where the contained value U is moved and converted to T, as if by T{std::move(U_value)}. If the source option is none, the the destination option will also be none.

This constructor only participates in overload resolution if the value type T of the destination option is convertible from the contained value of the source option U.

This constructor is explicit if U is not implicitly convertible to T.

Example

option<float> opt1{3.14};
option<int> opt2{std::move(opt1)};
assert(opt2);
assert(*opt2 == 3);

◆ option() [10/12]

constexpr CONDITIONALLY_EXPLICIT option ( [[maybe_unused] ] std::in_place_t  inplace,
Args &&...  args 
)
inlineconstexpr

Emplacement constructor.

The option is initialized such that it contains a value that is constructed in place from the forwarded arguments.

This constructor is explicit if inplace is the only argument.

Example

option<std::string> opt{std::in_place, 5, 'a'};
assert(opt);
assert(*opt == "aaaaa");

◆ option() [11/12]

constexpr option ( [[maybe_unused] ] std::in_place_t  inplace,
std::initializer_list< U >  init,
Args &&...  args 
)
inlineconstexpr

Emplacement constructor with initializer list.

The option is initialized such that it contains a value that is constructed in place from the forwarded arguments.

Example

option<std::vector<int>> opt{std::in_place, {1, 2, 3, 4, 5}};
assert(opt);
assert(opt->size() == 5);

◆ option() [12/12]

constexpr CONDITIONALLY_EXPLICIT option ( U &&  value)
inlineconstexpr

Forwarding constructor.

The option is initialized such that it contains a value that is constructed in place with the passed value forwarded to the value type's constructor.

This constructor only participates in overload resolution if the contained value type is constructible from the passed value, the passed value is not of type std::in_place_t, and either the the contained value type is not a scalar, or the forwarded value is not an option instance.

This constructor is explicit if the passed value is not implicitly convertible to T.

Example

float value = 3.14;
option<int> opt{value};
assert(opt);
assert(*opt == 3);

◆ ~option()

constexpr ~option ( )
constexpr

Destructor.

If the option is not none, the contained value is destroy in place.

The destructor is noexcept if the contained value type is nothrow destructible.

Member Function Documentation

◆ and_then() [1/4]

constexpr auto and_then ( F &&  f) &
inlineconstexpr

Applies a callable to the contents of an option.

If the option contains a value, the value is passed into the callable, and the invocation result is returned from this function. If the option is none, the callable is not invoked and none is returned.

This function only participates in overload resolution if the invocation result of the callable is an option.

Example

option<int> opt1{};
option<int> opt2{42};
option<int> opt3{-42};
auto callable = [](int value) -> option<unsigned int> {
if (value < 0) {
return none;
} else {
return static_cast<unsigned int>(value);
}
};
auto opt1_res = opt1.and_then(callable);
auto opt2_res = opt2.and_then(callable);
auto opt3_res = opt3.and_then(callable);
assert(!opt1_res.has_value());
assert(opt2_res.has_value());
assert(*opt2_res == 42);
assert(!opt3_res.has_value());
Parameters
fCallable applied to the contained option value

◆ and_then() [2/4]

constexpr auto and_then ( F &&  f) &&
inlineconstexpr

Applies a callable to the contents of an option.

If the option contains a value, the value is passed into the callable, and the invocation result is returned from this function. If the option is none, the callable is not invoked and none is returned.

This function only participates in overload resolution if the invocation result of the callable is an option.

Example

option<int> opt1{};
option<int> opt2{42};
option<int> opt3{-42};
auto callable = [](int value) -> option<unsigned int> {
if (value < 0) {
return none;
} else {
return static_cast<unsigned int>(value);
}
};
auto opt1_res = std::move(opt1).and_then(callable);
auto opt2_res = std::move(opt2).and_then(callable);
auto opt3_res = std::move(opt3).and_then(callable);
assert(!opt1_res.has_value());
assert(opt2_res.has_value());
assert(*opt2_res == 42);
assert(!opt3_res.has_value());
Parameters
fCallable applied to the contained option value

◆ and_then() [3/4]

constexpr auto and_then ( F &&  f) const &
inlineconstexpr

Applies a callable to the contents of an option.

If the option contains a value, the value is passed into the callable, and the invocation result is returned from this function. If the option is none, the callable is not invoked and none is returned.

This function only participates in overload resolution if the invocation result of the callable is an option.

Example

const option<int> opt1{};
const option<int> opt2{42};
const option<int> opt3{-42};
auto callable = [](int value) -> option<unsigned int> {
if (value < 0) {
return none;
} else {
return static_cast<unsigned int>(value);
}
};
auto opt1_res = opt1.and_then(callable);
auto opt2_res = opt2.and_then(callable);
auto opt3_res = opt3.and_then(callable);
assert(!opt1_res.has_value());
assert(opt2_res.has_value());
assert(*opt2_res == 42);
assert(!opt3_res.has_value());
Parameters
fCallable applied to the contained option value

◆ and_then() [4/4]

constexpr auto and_then ( F &&  f) const &&
inlineconstexpr

Applies a callable to the contents of an option.

If the option contains a value, the value is passed into the callable, and the invocation result is returned from this function. If the option is none, the callable is not invoked and none is returned.

This function only participates in overload resolution if the invocation result of the callable is an option.

Example

const option<int> opt1{};
const option<int> opt2{42};
const option<int> opt3{-42};
auto callable = [](int value) -> option<unsigned int> {
if (value < 0) {
return none;
} else {
return static_cast<unsigned int>(value);
}
};
auto opt1_res = std::move(opt1).and_then(callable);
auto opt2_res = std::move(opt2).and_then(callable);
auto opt3_res = std::move(opt3).and_then(callable);
assert(!opt1_res.has_value());
assert(opt2_res.has_value());
assert(*opt2_res == 42);
assert(!opt3_res.has_value());
Parameters
fCallable applied to the contained option value

◆ cref()

constexpr option<const_reference> cref ( ) const
inlineconstexprnoexcept

Converts an option into an option of a reference.

This function acts as a monadic operation that gets a reference to the contained value without unwrapping the option. In the case where the contained value is void, the returned option is also void.

Example

option<int> opt1{};
option<int> opt2{42};
option<const int&> opt1_ref = opt1.cref();
option<const int&> opt2_ref = opt2.cref();
assert(opt1_ref == nullptr);
assert(opt2_ref == &*opt2);

◆ emplace()

constexpr reference emplace ( Args &&...  args)
inlineconstexpr

Constructs a new value in place into the option.

If the option already contains a value, the old value is first destroyed in place. In any case, a new value is constructed in place in the option.

Example

option<std::string> opt1{};
option<std::string> opt2{"hello"};
opt1.emplace(5, 'a');
opt2.emplace(5, 'a');
assert(opt1.has_value());
assert(*opt1 == "aaaaa");
assert(opt2.has_value());
assert(*opt2 == "aaaaa");

◆ error_or() [1/6]

constexpr result<U, T> error_or ( const result< U, never > &  value) &&
inlineconstexpr

Converts an option to a result.

This function constructs and returns a result such that the contained value of the option becomes the error value of the result. If the option is none, the provided default value is used as the ok value of the result.

Example

option<int> opt1{};
option<int> opt2{42};
const auto value = ok<int>(-42);
result<int, int> res1 = std::move(opt1).error_or(value);
result<int, int> res2 = std::move(opt2).error_or(value);
assert(res1.has_value());
assert(*res1 == -42);
assert(!res2.has_value());
assert(res2.error() == 42);
Parameters
errThe default ok value used if the option is none.

◆ error_or() [2/6]

constexpr result<U, T> error_or ( const result< U, never > &  value) const &
inlineconstexpr

Converts an option to a result.

This function constructs and returns a result such that the contained value of the option becomes the error value of the result. If the option is none, the provided default value is used as the ok value of the result.

Example

option<int> opt1{};
option<int> opt2{42};
const auto value = ok<int>(-42);
result<int, int> res1 = opt1.error_or(value);
result<int, int> res2 = opt2.error_or(value);
assert(res1.has_value());
assert(*res1 == -42);
assert(!res2.has_value());
assert(res2.error() == 42);
Parameters
errThe default ok value used if the option is none.

◆ error_or() [3/6]

constexpr result<U, T> error_or ( result< U, never > &&  value) &&
inlineconstexpr

Converts an option to a result.

This function constructs and returns a result such that the contained value of the option becomes the error value of the result. If the option is none, the provided default value is used as the ok value of the result.

Example

option<int> opt1{};
option<int> opt2{42};
result<int, int> res1 = std::move(opt1).error_or(ok<int>(-42));
result<int, int> res2 = std::move(opt2).error_or(ok<int>(-42));
assert(res1.has_value());
assert(*res1 == -42);
assert(!res2.has_value());
assert(res2.error() == 42);
Parameters
errThe default ok value used if the option is none.

◆ error_or() [4/6]

constexpr result<U, T> error_or ( result< U, never > &&  value) const &
inlineconstexpr

Converts an option to a result.

This function constructs and returns a result such that the contained value of the option becomes the error value of the result. If the option is none, the provided default value is used as the ok value of the result.

Example

option<int> opt1{};
option<int> opt2{42};
result<int, int> res1 = opt1.error_or(ok<int>(-42));
result<int, int> res2 = opt2.error_or(ok<int>(-42));
assert(res1.has_value());
assert(*res1 == -42);
assert(!res2.has_value());
assert(res2.error() == 42);
Parameters
errThe default ok value used if the option is none.

◆ error_or() [5/6]

constexpr result<std::remove_cvref_t<U>, T> error_or ( U &&  value) &&
inlineconstexpr

Converts an option to a result.

This function constructs and returns a result such that the contained value of the option becomes the error value of the result. If the option is none, the provided default value is used as the ok value of the result.

The default ok value is always converted to a non-reference. If a reference is desired, use the overload of this function that accepts an ok_t.

This function only participates in overload resolution if U is not an ok_t.

Example

option<int> opt1{};
option<int> opt2{42};
result<int, int> res1 = std::move(opt1).error_or(-42);
result<int, int> res2 = std::move(opt2).error_or(-42);
assert(res1.has_value());
assert(*res1 == -42);
assert(!res2.has_value());
assert(res2.error() == 42);
Parameters
errThe default ok value used if the option is none.

◆ error_or() [6/6]

constexpr result<std::remove_cvref_t<U>, T> error_or ( U &&  value) const &
inlineconstexpr

Converts an option to a result.

This function constructs and returns a result such that the contained value of the option becomes the error value of the result. If the option is none, the provided default value is used as the ok value of the result.

The default ok value is always converted to a non-reference. If a reference is desired, use the overload of this function that accepts an ok_t.

This function only participates in overload resolution if U is not an ok_t.

Example

option<int> opt1{};
option<int> opt2{42};
result<int, int> res1 = opt1.error_or(-42);
result<int, int> res2 = opt2.error_or(-42);
assert(res1.has_value());
assert(*res1 == -42);
assert(!res2.has_value());
assert(res2.error() == 42);
Parameters
errThe default ok value used if the option is none.

◆ error_or_else() [1/2]

constexpr result<std::invoke_result_t<F>, T> error_or_else ( F &&  f) &&
inlineconstexpr

Converts an option to a result.

This fucntion constructs and returns a result such that the contained value of the option becomes the error value of the result. If the option is none, the result of invoking the provided callable is used as the ok value of the result.

This function might be preferred over .error_or(ok_value) when the value type is expensive to construct, copy, or move. This function allows the value to never be instantiated if the option is not none.

Example

option<int> opt1{};
option<int> opt2{42};
result<int, int> res1 = std::move(opt1).error_or_else([] {
return -42;
});
result<int, int> res2 = std::move(opt2).error_or_else([] {
return -42;
});
assert(res1.has_value());
assert(*res1 == -42);
assert(!res2.has_value());
assert(res2.error() == 42);
Parameters
fA callable that creates a default ok value.

◆ error_or_else() [2/2]

constexpr result<std::invoke_result_t<F>, T> error_or_else ( F &&  f) const &
inlineconstexpr

Converts an option to a result.

This fucntion constructs and returns a result such that the contained value of the option becomes the error value of the result. If the option is none, the result of invoking the provided callable is used as the ok value of the result.

This function might be preferred over .error_or(ok_value) when the value type is expensive to construct, copy, or move. This function allows the value to never be instantiated if the option is not none.

Example

option<int> opt1{};
option<int> opt2{42};
result<int, int> res1 = opt1.error_or_else([] { return -42; });
result<int, int> res2 = opt2.error_or_else([] { return -42; });
assert(res1.has_value());
assert(*res1 == -42);
assert(!res2.has_value());
assert(res2.error() == 42);
Parameters
fA callable that creates a default ok value.

◆ filter() [1/2]

constexpr option filter ( Pred &&  pred) &&
inlineconstexpr

Filters the option based on a predicate.

If the option is already none, none is returned. If the option contains a value, the returned value depends on the predicate. If the predicate returns true, an option with the moved original contained value is returned. Otherwise, none is returned.

The predicate must have a signature compatible with: bool pred(T&) or bool pred(const T&).

Example

option<int> opt1{};
option<int> opt2{42};
option<int> opt3{-42};
auto opt1_filt = std::move(opt1).filter([](int val) {
return val > 0;
});
auto opt2_filt = std::move(opt2).filter([](int val) {
return val > 0;
});
auto opt3_filt = std::move(opt3).filter([](int val) {
return val > 0;
});
assert(opt1_filt.is_none());
assert(opt2_filt.is_some());
assert(*opt2_filt == 42);
assert(opt3_filt.is_none());

◆ filter() [2/2]

constexpr option filter ( Pred &&  pred) const &
inlineconstexpr

Filters the option based on a predicate.

If the option is already none, none is returned. If the option contains a value, the returned value depends on the predicate. If the predicate returns true, an option with a copy of the original contained value is returned. Otherwise, none is returned.

The predicate must have a signature compatible with: bool pred(const T&).

Example

option<int> opt1{};
option<int> opt2{42};
option<int> opt3{-42};
auto opt1_filt = opt1.filter([](int val) { return val > 0; });
auto opt2_filt = opt2.filter([](int val) { return val > 0; });
auto opt3_filt = opt3.filter([](int val) { return val > 0; });
assert(opt1_filt.is_none());
assert(opt2_filt.is_some());
assert(*opt2_filt == 42);
assert(opt3_filt.is_none());

◆ flatten() [1/2]

constexpr T flatten ( ) &&
inlineconstexpr

Converts option<option<T>> to option<T>

If the outer option is none or the inner option is none, none is returned. Otherwise the flattend option with a contained value is returned.

Note that only one level of option is removed. An option<option<option<T>>> will flatten to an option<option<T>>.

This function only participates in overload resolution if T is an option.

Example

option<option<int>> opt1{};
option<option<int>> opt2 = some<option<int>>(none);
option<option<int>> opt3 = some<option<int>>(42);
auto opt1_flat = std::move(opt1).flatten();
auto opt2_flat = std::move(opt2).flatten();
auto opt3_flat = std::move(opt3).flatten();
assert(opt1_flat.is_none());
assert(opt2_flat.is_none());
assert(opt3_flat.is_some());
assert(*opt3_flat = 42);

◆ flatten() [2/2]

constexpr T flatten ( ) const &
inlineconstexpr

Converts option<option<T>> to option<T>

If the outer option is none or the inner option is none, none is returned. Otherwise the flattend option with a contained value is returned.

Note that only one level of option is removed. An option<option<option<T>>> will flatten to an option<option<T>>.

This function only participates in overload resolution if T is an option.

Example

option<option<int>> opt1{};
option<option<int>> opt2 = some<option<int>>(none);
option<option<int>> opt3 = some<option<int>>(42);
auto opt1_flat = opt1.flatten();
auto opt2_flat = opt2.flatten();
auto opt3_flat = opt3.flatten();
assert(opt1_flat.is_none());
assert(opt2_flat.is_none());
assert(opt3_flat.is_some());
assert(*opt3_flat = 42);

◆ flatten_all() [1/2]

constexpr auto flatten_all ( ) &&
inlineconstexpr

Converts nested option types into a single option.

Like flatten, this function removes nested layers of option, such as converting option<option<T>> into option<T>. However, unlike flatten, this function removes all layers of nesting and works for unnested option types. For example, option<option<option<T>>> is converted directly to option<T>, and for option<T>, where T is not an option type, no layers of option are removed.

Example

option<option<option<int>>> opt1{in_place, in_place, in_place, 42};
option<option<option<int>>> opt2{none};
option<option<option<int>>> opt3{in_place, none};
option<option<option<int>>> opt4{in_place, in_place, none};
option<int> opt1_flat = std::move(opt1).flatten_all();
option<int> opt2_flat = std::move(opt2).flatten_all();
option<int> opt3_flat = std::move(opt3).flatten_all();
option<int> opt4_flat = std::move(opt4).flatten_all();
assert(opt1_flat.is_some());
assert(*opt1_flat == 42);
assert(opt2_flat.is_none());
assert(opt3_flat.is_none());
assert(opt4_flat.is_none());

◆ flatten_all() [2/2]

constexpr auto flatten_all ( ) const &
inlineconstexpr

Converts nested option types into a single option.

Like flatten, this function removes nested layers of option, such as converting option<option<T>> into option<T>. However, unlike flatten, this function removes all layers of nesting and works for unnested option types. For example, option<option<option<T>>> is converted directly to option<T>, and for option<T>, where T is not an option type, no layers of option are removed.

Example

option<option<option<int>>> opt1{in_place, in_place, in_place, 42};
option<option<option<int>>> opt2{none};
option<option<option<int>>> opt3{in_place, none};
option<option<option<int>>> opt4{in_place, in_place, none};
option<int> opt1_flat = opt1.flatten_all();
option<int> opt2_flat = opt2.flatten_all();
option<int> opt3_flat = opt3.flatten_all();
option<int> opt4_flat = opt4.flatten_all();
assert(opt1_flat.is_some());
assert(*opt1_flat == 42);
assert(opt2_flat.is_none());
assert(opt3_flat.is_none());
assert(opt4_flat.is_none());

◆ has_value()

constexpr bool has_value ( ) const
inlineconstexprnoexcept

Returns true if the option contains a value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(opt1.has_value() == false);
assert(opt2.has_value() == true);

◆ is_none()

constexpr bool is_none ( ) const
inlineconstexprnoexcept

Returns true if the options does not contain a value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(opt1.is_none() == true);
assert(opt2.is_none() == false);

◆ is_some()

constexpr bool is_some ( ) const
inlineconstexprnoexcept

Returns true if the option contains a value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(opt1.is_some() == false);
assert(opt2.is_some() == true);

◆ map() [1/4]

constexpr auto map ( F &&  f) &
inlineconstexpr

Performs a transformation on the content of an option.

This function is a monadic operation that passes the value contained in the option, if any, to the callable f. The value returned by the call to f is then returned from this function wrapped in a new option. If the original option is none, the returned value is a none option of the type that would have been returned by f.

Note that this function is identical to transform, but the name map is the more typical name of this monadic operation outside of C++.

Example

option<int> opt1{};
option<int> opt2{42};
auto opt1_mapped = opt1.map([](auto value) {
return std::to_string(value);
});
auto opt2_mapped = opt2.map([](auto value) {
return std::to_string(value);
});
assert(!opt1_mapped.has_value());
assert(opt2_mapped.has_value());
assert(*opt2_mapped = "42");
Parameters
fA callable that maps the option value

◆ map() [2/4]

constexpr auto map ( F &&  f) &&
inlineconstexpr

Performs a transformation on the content of an option.

This function is a monadic operation that passes the value contained in the option, if any, to the callable f. The value returned by the call to f is then returned from this function wrapped in a new option. If the original option is none, the returned value is a none option of the type that would have been returned by f.

Note that this function is identical to transform, but the name map is the more typical name of this monadic operation outside of C++.

Example

option<int> opt1{};
option<int> opt2{42};
auto opt1_mapped = std::move(opt1).map([](auto value) {
return std::to_string(value);
});
auto opt2_mapped = std::move(opt2).map([](auto value) {
return std::to_string(value);
});
assert(!opt1_mapped.has_value());
assert(opt2_mapped.has_value());
assert(*opt2_mapped = "42");
Parameters
fA callable that maps the option value

◆ map() [3/4]

constexpr auto map ( F &&  f) const &
inlineconstexpr

Performs a transformation on the content of an option.

This function is a monadic operation that passes the value contained in the option, if any, to the callable f. The value returned by the call to f is then returned from this function wrapped in a new option. If the original option is none, the returned value is a none option of the type that would have been returned by f.

Note that this function is identical to transform, but the name map is the more typical name of this monadic operation outside of C++.

Example

const option<int> opt1{};
const option<int> opt2{42};
auto opt1_mapped = opt1.map([](auto value) {
return std::to_string(value);
});
auto opt2_mapped = opt2.map([](auto value) {
return std::to_string(value);
});
assert(!opt1_mapped.has_value());
assert(opt2_mapped.has_value());
assert(*opt2_mapped = "42");
Parameters
fA callable that maps the option value

◆ map() [4/4]

constexpr auto map ( F &&  f) const &&
inlineconstexpr

Performs a transformation on the content of an option.

This function is a monadic operation that passes the value contained in the option, if any, to the callable f. The value returned by the call to f is then returned from this function wrapped in a new option. If the original option is none, the returned value is a none option of the type that would have been returned by f.

Note that this function is identical to transform, but the name map is the more typical name of this monadic operation outside of C++.

Example

const option<int> opt1{};
const option<int> opt2{42};
auto opt1_mapped = std::move(opt1).map([](auto value) {
return std::to_string(value);
});
auto opt2_mapped = std::move(opt2).map([](auto value) {
return std::to_string(value);
});
assert(!opt1_mapped.has_value());
assert(opt2_mapped.has_value());
assert(*opt2_mapped = "42");
Parameters
fA callable that maps the option value

◆ ok_or() [1/6]

constexpr result<T, E> ok_or ( const result< never, E > &  err) &&
inlineconstexpr

Converts an option to a result.

This function constructs and returns a result such that the contained value of the option becomes the ok value of the result. If the option is none, the provided error value is used as the error value of the result.

Example

option<int> opt1{};
option<int> opt2{42};
const auto err = error<int>(-42);
result<int, int> res1 = std::move(opt1).ok_or(err);
result<int, int> res2 = std::move(opt2).ok_or(err);
assert(!res1.has_value());
assert(res1.error() == -42);
assert(res2.has_value());
assert(*res2 == 42);
Parameters
errThe default error value used if the option is none.

◆ ok_or() [2/6]

constexpr result<T, E> ok_or ( const result< never, E > &  err) const &
inlineconstexpr

Converts an option to a result.

This function constructs and returns a result such that the contained value of the option becomes the ok value of the result. If the option is none, the provided error value is used as the error value of the result.

Example

option<int> opt1{};
option<int> opt2{42};
const auto err = error<int>(-42);
result<int, int> res1 = opt1.ok_or(err);
result<int, int> res2 = opt2.ok_or(err);
assert(!res1.has_value());
assert(res1.error() == -42);
assert(res2.has_value());
assert(*res2 == 42);
Parameters
errThe default error value used if the option is none.

◆ ok_or() [3/6]

constexpr result<T, std::remove_cvref_t<E> > ok_or ( E &&  err) &&
inlineconstexpr

Converts an option to a result.

This function constructs and returns a result such that the contained value of the option becomes the ok value of the result. If the option is none, the provided default value is used as the error value of the result.

The default error value is always converted to a non-reference. If a reference is desired, use the overload of this function that accepts an error_t.

This function only participates in overload resolution if E is not an error_t.

Example

option<int> opt1{};
option<int> opt2{42};
result<int, int> res1 = std::move(opt1).ok_or(-42);
result<int, int> res2 = std::move(opt2).ok_or(-42);
assert(!res1.has_value());
assert(res1.error() == -42);
assert(res2.has_value());
assert(*res2 == 42);
Parameters
errThe default error value used if the option is none.

◆ ok_or() [4/6]

constexpr result<T, std::remove_cvref_t<E> > ok_or ( E &&  err) const &
inlineconstexpr

Converts an option to a result.

This function constructs and returns a result such that the contained value of the option becomes the ok value of the result. If the option is none, the provided default value is used as the error value of the result.

The default error value is always converted to a non-reference. If a reference is desired, use the overload of this function that accepts an error_t.

This function only participates in overload resolution if E is not an error_t.

Example

option<int> opt1{};
option<int> opt2{42};
result<int, int> res1 = opt1.ok_or(-42);
result<int, int> res2 = opt2.ok_or(-42);
assert(!res1.has_value());
assert(res1.error() == -42);
assert(res2.has_value());
assert(*res2 == 42);
Parameters
errThe default error value used if the option is none.

◆ ok_or() [5/6]

constexpr result<T, E> ok_or ( result< never, E > &&  err) &&
inlineconstexpr

Converts an option to a result.

This function constructs and returns a result such that the contained value of the option becomes the ok value of the result. If the option is none, the provided error value is used as the error value of the result.

Example

option<int> opt1{};
option<int> opt2{42};
result<int, int> res1 = std::move(opt1).ok_or(error<int>(-42));
result<int, int> res2 = std::move(opt2).ok_or(error<int>(-42));
assert(!res1.has_value());
assert(res1.error() == -42);
assert(res2.has_value());
assert(*res2 == 42);
Parameters
errThe default error value used if the option is none.

◆ ok_or() [6/6]

constexpr result<T, E> ok_or ( result< never, E > &&  err) const &
inlineconstexpr

Converts an option to a result.

This function constructs and returns a result such that the contained value of the option becomes the ok value of the result. If the option is none, the provided error value is used as the error value of the result.

Example

option<int> opt1{};
option<int> opt2{42};
result<int, int> res1 = opt1.ok_or(error<int>(-42));
result<int, int> res2 = opt2.ok_or(error<int>(-42));
assert(!res1.has_value());
assert(res1.error() == -42);
assert(res2.has_value());
assert(*res2 == 42);
Parameters
errThe default error value used if the option is none.

◆ ok_or_else() [1/2]

constexpr result<T, std::invoke_result_t<F> > ok_or_else ( F &&  f) &&
inlineconstexpr

Converts an option to a result.

This fucntion constructs and returns a result such that the contained value of the option becomes the ok value of the result. If the option is none, the result of invoking the provided callable is used as the error value of the result.

This function might be preferred over .ok_or(error_value) when the error type is expense to construct, copy, or move. This function allows the error value to never be instantiated if the option is not none.

Example

option<int> opt1{};
option<int> opt2{42};
result<int, int> res1 = std::move(opt1).ok_or_else([] { return -42; });
result<int, int> res2 = std::move(opt2).ok_or_else([] { return -42; });
assert(!res1.has_value());
assert(res1.error() == -42);
assert(res2.has_value());
assert(*res2 == 42);
Parameters
fA callable that creates a default error value.

◆ ok_or_else() [2/2]

constexpr result<T, std::invoke_result_t<F> > ok_or_else ( F &&  f) const &
inlineconstexpr

Converts an option to a result.

This fucntion constructs and returns a result such that the contained value of the option becomes the ok value of the result. If the option is none, the result of invoking the provided callable is used as the error value of the result.

This function might be preferred over .ok_or(error_value) when the error type is expense to construct, copy, or move. This function allows the error value to never be instantiated if the option is not none.

Example

option<int> opt1{};
option<int> opt2{42};
result<int, int> res1 = opt1.ok_or_else([] { return -42; });
result<int, int> res2 = opt2.ok_or_else([] { return -42; });
assert(!res1.has_value());
assert(res1.error() == -42);
assert(res2.has_value());
assert(*res2 == 42);
Parameters
fA callable that creates a default error value.

◆ operator bool()

constexpr operator bool ( ) const
inlineconstexprnoexcept

Implicit conversion to bool.

This implicit conversion allows an option to be used directly in a condition to check if the option contains a value.

Example

option<int> opt1{};
option<int> opt2{42};
if (opt1) {
assert(false);
} else {
assert(true);
}
if (opt2) {
assert(true);
} else {
assert(false);
}

◆ operator U*()

constexpr CONDITIONALLY_EXPLICIT operator U* ( ) const
inlineconstexprnoexcept

Conversion to raw pointer.

This conversion allows an option of an lvalue to be converted into a raw pointer, where the resulting pointer points to the referenced value or is null if the option is none.

This conversion only participates in overload resolution if T is an lvalue reference and pointer can be cast to U*.

This conversion is explicit if pointer is not implicitly convertible to U*.

Example

int value = 42;
option<int&> opt{value};
int* ptr = opt;
assert(ptr == &value);

◆ operator*() [1/4]

constexpr rvalue_reference operator* ( ) &&
inlineconstexpr

Accesses the value contained in the option.

This operator does not check if the option contains a value. Use of this operator when the option is none results in undefined behavior.

Example

option<int> opt{42};
assert(*std::move(opt) == 42);

◆ operator*() [2/4]

constexpr reference operator* ( ) &
inlineconstexprnoexcept

Accesses the value contained in the option.

This operator does not check if the option contains a value. Use of this operator when the option is none results in undefined behavior.

Example

option<int> opt{42};
assert(*opt == 42);

◆ operator*() [3/4]

constexpr const_rvalue_reference operator* ( ) const &&
inlineconstexpr

Accesses the value contained in the option.

This operator does not check if the option contains a value. Use of this operator when the option is none results in undefined behavior.

Example

const option<int> opt{42};
assert(*std::move(opt) == 42);

◆ operator*() [4/4]

constexpr const_reference operator* ( ) const &
inlineconstexprnoexcept

Accesses the value contained in the option.

This operator does not check if the option contains a value. Use of this operator when the option is none results in undefined behavior.

Example

const option<int> opt{42};
assert(*opt == 42);

◆ operator->() [1/2]

constexpr const_pointer operator-> ( ) const
inlineconstexprnoexcept

Accesses members of the value contained in the option.

This operator does not check if the option contains a value. Use of this operator when the option is none results in undefined behavior.

Example

const option<std::string> opt{"hello"};
assert(opt->size() == 5);

◆ operator->() [2/2]

constexpr pointer operator-> ( )
inlineconstexprnoexcept

Accesses members of the value contained in the option.

This operator does not check if the option contains a value. Use of this operator when the option is none results in undefined behavior.

Example

option<std::string> opt{"hello"};
assert(opt->size() == 5);

◆ operator=() [1/9]

constexpr option& operator= ( [[maybe_unused] ] none_t  null)
inlineconstexpr

none assignment operator

If the option contains a value, the value is destroyed in place. If the option is already none, this function has no effect.

This function is noexcept if the contained value type is nothrow destructible.

Example

option<int> opt{42};
opt = none;
assert(!opt);

◆ operator=() [2/9]

constexpr option& operator= ( [[maybe_unused] ] std::nullopt_t  null)
inlineconstexpr

std::nullopt assignment operator

If the option contains a value, the value is destroyed in place. If the option is already none, this function has no effect.

This function is noexcept if the contained value type is nothrow destructible.

Example

option<int> opt{42};
opt = std::nullopt;
assert(!opt);

◆ operator=() [3/9]

constexpr option& operator= ( [[maybe_unused] ] std::nullptr_t  null)
inlineconstexprnoexcept

nullptr assignment operator

Sets an lvalue reference option to none.

This function only participates in overload resolution if the contained value type is an lvalue reference.

Example

int value = 42;
option<int&> opt{value};
opt = nullptr;
assert(!opt);

◆ operator=() [4/9]

constexpr option& operator= ( const option< T > &  )
constexpr

Copy assignment operator.

The destination option is reassigned such that it contains the value of the source option, if it has any.

If both the source and desitnation option are not none, then the value contained in the destination option is copy assigned from the value contained in the source option.

If the source option contains a value and the destination is none, the value contained in the source option is destroyed in place, making the destination option none.

If the source option is none and the destination contains a value, a new value is copy constructed in place in the source option.

If both the source and destination option are none, both remain none.

This function is noexcept if the contained value type is nothrow copy assignable, nothrow copy constructible, and nothrow destructible.

Example

option<int> opt1{42};
option<int> opt2{};
opt2 = opt1;
assert(opt2);
assert(*opt2 == 42);
opt1 = none;
opt2 = opt1;
assert(!opt2);

◆ operator=() [5/9]

constexpr option& operator= ( const option< U > &  value)
inlineconstexpr

Converting copy assignment operator.

This function performs copy assignment just like the standard copy assignment operator, but it also performs conversion from the value contained in the source option to the value type of contained in the destination option.

This function only participates in overload resolution if T is not construcitble from, convertible from, or assignable from any cvref qualified form of option<U>, and T is both constructible and assignable from U.

Example

option<float> opt1{3.14};
option<int> opt2{};
opt2 = opt1;
assert(opt2);
assert(*opt2 == 3);

◆ operator=() [6/9]

constexpr option& operator= ( option< T > &&  )
constexpr

Move assignment operator.

The destination option is reassigned such that it contains the moved value of the source option, if it has any. In all cases, the source option will be none after the move.

If both the source and destination option are not none, then the value contained in the destination option is move assigned from the moved value from the source option. The old value in the source option is destroyed in place.

If the source option contains a value and the desitnation is none, the value contained in the source option is destroyed in place, making the desitnation option none.

If the source option is none and the destination contains a value, a new value is move constructed in place in the source option. The old value in the source option is destroyed in place.

If both the source and destination option are none, both remain none.

This function is noexcept if the contained value type is nothrow move assignable, nothrow move constructible, and nothrow destructible.

Example

option<int> opt1{42};
option<int> opt2{};
opt2 = std::move(opt1);
assert(!opt1);
assert(opt2);
assert(*opt2 == 42);
opt1 = std::move(opt2);
assert(!opt2);
assert(opt1);
assert(*opt1 == 42);

◆ operator=() [7/9]

constexpr option& operator= ( option< U > &&  value)
inlineconstexpr

Converting move assignment operator.

This function performs move assignment just like the standard move assignment operator, but it also performs conversion from the value contained in the source option to the value type of contained in the destination option.

This function only participates in overload resolution if T is not construcitble from, convertible from, or assignable from any cvref qualified form of option<U>, and T is both constructible and assignable from U.

Example

option<float> opt1{3.14};
option<int> opt2{};
opt2 = std::move(opt1);
assert(opt2);
assert(*opt2 == 3);

◆ operator=() [8/9]

constexpr option& operator= ( U &&  value)
inlineconstexpr

Value assignment operator.

Sets the option to contain the provided value, converted to T. If the option holds a value (is not none) before the assigment, the value is directly assigned the forwarded source value. If the option is none, a new value is constructed in place from the forwarded source value.

This function only participates in overload resolution if:

  • The source value is not an option
  • T is constructible from U
  • T is assignable from U
  • T is not scalar or U is not equivalent to T

Example

option<long long> opt{};
opt = 42;
assert(opt);
assert(*opt == 42);

◆ operator=() [9/9]

constexpr option& operator= ( U *  ptr)
inlineconstexprnoexcept

Pointer assignment operator.

The destination option is assigned such that it contains an lvalue reference that the source pointer points to. If the pointer is null, the destination option will be none.

This function only participates in overload resolution if T is an lvalue reference and the pointer U* can be implicitly converted to pointer.

Example

int value = 42;
int* ptr = &value;
option<int&> opt{};
opt = ptr;
assert(opt)
assert(opt == ptr);

◆ or_else() [1/2]

constexpr option or_else ( F &&  f) &&
inlineconstexpr

Returns the result of f if the option is none.

If the option contains a value, a copy of the option is returned. Otherwise, the result of invoking f is returned.

Example

option<int> opt1{};
option<int> opt2{42};
auto opt1_new = std::move(opt1).or_else([] { return 0; });
auto opt2_new = std::move(opt2).or_else([] { return 0; });
assert(opt1_new.has_value());
assert(*opt1_new == 0);
assert(opt2_new.has_value());
assert(*opt2_new == 42);
Parameters
fA callable that returns the "else" option

◆ or_else() [2/2]

constexpr option or_else ( F &&  f) const &
inlineconstexpr

Returns the result of f if the option is none.

If the option contains a value, a copy of the option is returned. Otherwise, the result of invoking f is returned.

Example

option<int> opt1{};
option<int> opt2{42};
auto opt1_new = opt1.or_else([] { return 0; });
auto opt2_new = opt2.or_else([] { return 0; });
assert(opt1_new.has_value());
assert(*opt1_new == 0);
assert(opt2_new.has_value());
assert(*opt2_new == 42);
Parameters
fA callable that returns the "else" option

◆ ref() [1/2]

constexpr option<const_reference> ref ( ) const
inlineconstexprnoexcept

Converts an option into an option of a reference.

This function acts as a monadic operation that gets a reference to the contained value without unwrapping the option. In the case where the contained value is void, the returned option is also void.

Example

const option<int> opt1{};
const option<int> opt2{42};
option<const int&> opt1_ref = opt1.ref();
option<const int&> opt2_ref = opt2.ref();
assert(opt1_ref == nullptr);
assert(opt2_ref == &*opt2);

◆ ref() [2/2]

constexpr option<reference> ref ( )
inlineconstexprnoexcept

Converts an option into an option of a reference.

This function acts as a monadic operation that gets a reference to the contained value without unwrapping the option. In the case where the contained value is void, the returned option is also void.

Example

option<int> opt1{};
option<int> opt2{42};
option<int&> opt1_ref = opt1.ref();
option<int&> opt2_ref = opt2.ref();
assert(opt1_ref == nullptr);
assert(opt2_ref == &*opt2);

◆ reset()

constexpr void reset ( )
inlineconstexprnoexcept

Sets the option to none.

If the option contains a value, the value is destroyed in place. Otherwise, nothing is done.

Example

option<int> opt1{};
option<int> opt2{42};
opt1.reset();
opt2.reset();
assert(!opt1.has_value());
assert(!opt2.has_value());

◆ some() [1/4]

constexpr reference some ( ) &
inlineconstexpr

Accesses the value contained in the option.

This function first checks if the option contains a value before attempting to access the value. If the option is none, then this function throws an exception.

Example

option<int> opt{42};
assert(opt.some() == 42);
Exceptions
bad_option_accessThrown if the option is none.

◆ some() [2/4]

constexpr rvalue_reference some ( ) &&
inlineconstexpr

Accesses the value contained in the option.

This function first checks if the option contains a value before attempting to access the value. If the option is none, then this function throws an exception.

Example

option<int> opt{42};
assert(std::move(opt).some() == 42);
constexpr reference some() &
Accesses the value contained in the option.
Definition: option.hpp:1282
Exceptions
bad_option_accessThrown if the option is none.

◆ some() [3/4]

constexpr const_reference some ( ) const &
inlineconstexpr

Accesses the value contained in the option.

This function first checks if the option contains a value before attempting to access the value. If the option is none, then this function throws an exception.

Example

const option<int> opt{42};
assert(opt.some() == 42);
Exceptions
bad_option_accessThrown if the option is none.

◆ some() [4/4]

constexpr rvalue_reference some ( ) const &&
inlineconstexpr

Accesses the value contained in the option.

This function first checks if the option contains a value before attempting to access the value. If the option is none, then this function throws an exception.

Example

const option<int> opt{42};
assert(std::move(opt).some() == 42);
Exceptions
bad_option_accessThrown if the option is none.

◆ some_or() [1/2]

constexpr value_type some_or ( U &&  default_value) &&
inlineconstexpr

Gets the option value with a default used for none.

If the option contains a value, the moved value is returned. If the option is none, the provided default value is returned as static_cast<T>(std::forward<U>(default_value)).

Example

option<int> opt1{};
option<int> opt2{42};
assert(std::move(opt1).some_or(0) == 0);
assert(std::move(opt2).some_or(0) == 42);
constexpr value_type some_or(U &&default_value) const &
Gets the option value with a default used for none.
Definition: option.hpp:1356
Parameters
default_valueThe value to use if the option is none.

◆ some_or() [2/2]

constexpr value_type some_or ( U &&  default_value) const &
inlineconstexpr

Gets the option value with a default used for none.

If the option contains a value, a copy of the value is returned. If the option is none, the provided default value is returned as static_cast<T>(std::forward<U>(default_value)).

Example

option<int> opt1{};
option<int> opt2{42};
assert(opt1.some_or(0) == 0);
assert(opt2.some_or(0) == 42);
Parameters
default_valueThe value to use if the option is none.

◆ some_or_else() [1/2]

constexpr value_type some_or_else ( F &&  f) &&
inlineconstexpr

Gets the option value with a default used for none.

If the option contains a value, the moved value is returned. If the option is none, the result of invoking f is returned. The returned value of f is also explicitly cast to value_type before return.

This function might be preferred over .value_or(default_value) when the default value is expensive to create or move. This function allows the default value to never be instantiated if the option is not none.

Example

option<int> opt1{};
option<int> opt2{42};
assert(std::move(opt1).some_or_else([] { return 0; }) == 0);
assert(std::move(opt2).some_or_else([] { return 0; }) == 42);
constexpr value_type some_or_else(F &&f) const &
Gets the option value with a default used for none.
Definition: option.hpp:1408
Parameters
fA callable that creates a default value.

◆ some_or_else() [2/2]

constexpr value_type some_or_else ( F &&  f) const &
inlineconstexpr

Gets the option value with a default used for none.

If the option contains a value, the copied value is returned. If the option is none, the result of invoking f is returned. The returned value of f is also explicitly cast to value_type before return.

This function might be preferred over .value_or(default_value) when the default value is expensive to create or move. This function allows the default value to never be instantiated if the option is not none.

Example

option<int> opt1{};
option<int> opt2{42};
assert(opt1.some_or_else([] { return 0; }) == 0);
assert(opt2.some_or_else([] { return 0; }) == 42);
Parameters
fA callable that creates a default value.

◆ swap()

constexpr void swap ( option< T > &  other)
inlineconstexpr

Swaps two option instances.

If both option instances contain a value, the two contained values are swapped directly. Otherwise, if one option contains a value, that value is move constructed into the other option, and the old value is destroyed in place. If both option instances are none, nothing is done.

This function is noexcept if the contained value type is nothrow swappable.

Example

option<int> opt1{};
option<int> opt2{42};
opt1.swap(opt2);
assert(opt1.has_value());
assert(*opt1 == 42);
assert(!opt1.has_value());

◆ transform() [1/4]

constexpr auto transform ( F &&  f) &
inlineconstexpr

Performs a transformation on the content of an option.

This function is a monadic operation that passes the value contained in the option, if any, to the callable f. The value returned by the call to f is then returned from this function wrapped in a new option. If the original option is none, the returned value is a none option of the type that would have been returned by f.

Note that this function is identical to map, but the name transform make option able to be a drop in replacement for std::optional. map is the more typical name of this monadic operation outside of C++.

Example

option<int> opt1{};
option<int> opt2{42};
auto opt1_mapped = opt1.transform([](auto value) {
return std::to_string(value);
});
auto opt2_mapped = opt2.transform([](auto value) {
return std::to_string(value);
});
assert(!opt1_mapped.has_value());
assert(opt2_mapped.has_value());
assert(*opt2_mapped = "42");
Parameters
fA callable that transforms the option value

◆ transform() [2/4]

constexpr auto transform ( F &&  f) &&
inlineconstexpr

Performs a transformation on the content of an option.

This function is a monadic operation that passes the value contained in the option, if any, to the callable f. The value returned by the call to f is then returned from this function wrapped in a new option. If the original option is none, the returned value is a none option of the type that would have been returned by f.

Note that this function is identical to map, but the name transform make option able to be a drop in replacement for std::optional. map is the more typical name of this monadic operation outside of C++.

Example

option<int> opt1{};
option<int> opt2{42};
auto opt1_mapped = std::move(opt1).transform([](auto value) {
return std::to_string(value);
});
auto opt2_mapped = std::move(opt2).transform([](auto value) {
return std::to_string(value);
});
assert(!opt1_mapped.has_value());
assert(opt2_mapped.has_value());
assert(*opt2_mapped = "42");
Parameters
fA callable that transforms the option value

◆ transform() [3/4]

constexpr auto transform ( F &&  f) const &
inlineconstexpr

Performs a transformation on the content of an option.

This function is a monadic operation that passes the value contained in the option, if any, to the callable f. The value returned by the call to f is then returned from this function wrapped in a new option. If the original option is none, the returned value is a none option of the type that would have been returned by f.

Note that this function is identical to map, but the name transform make option able to be a drop in replacement for std::optional. map is the more typical name of this monadic operation outside of C++.

Example

const option<int> opt1{};
const option<int> opt2{42};
auto opt1_mapped = opt1.transform([](auto value) {
return std::to_string(value);
});
auto opt2_mapped = opt2.transform([](auto value) {
return std::to_string(value);
});
assert(!opt1_mapped.has_value());
assert(opt2_mapped.has_value());
assert(*opt2_mapped = "42");
Parameters
fA callable that transforms the option value

◆ transform() [4/4]

constexpr auto transform ( F &&  f) const &&
inlineconstexpr

Performs a transformation on the content of an option.

This function is a monadic operation that passes the value contained in the option, if any, to the callable f. The value returned by the call to f is then returned from this function wrapped in a new option. If the original option is none, the returned value is a none option of the type that would have been returned by f.

Note that this function is identical to map, but the name transform make option able to be a drop in replacement for std::optional. map is the more typical name of this monadic operation outside of C++.

Example

const option<int> opt1{};
const option<int> opt2{42};
auto opt1_mapped = std::move(opt1).transform([](auto value) {
return std::to_string(value);
});
auto opt2_mapped = std::move(opt2).transform([](auto value) {
return std::to_string(value);
});
assert(!opt1_mapped.has_value());
assert(opt2_mapped.has_value());
assert(*opt2_mapped = "42");
Parameters
fA callable that transforms the option value

◆ transpose() [1/2]

constexpr auto transpose ( ) &&
inlineconstexpr

Converts option<result<T, E>> into result<option<T>, E>

If the option is none, a result containing a none option is returned. If the option contains an ok result, a result containing an ok value of an option with a value is returned. If the option contains a result with an error value, a result with an error value is returned.

This function only participates in overload resolution if T is a result.

Example

option<result<int, std::string>> val1{};
option<result<int, std::string>> val2{ok<int>(42)};
option<result<int, std::string>> val3{error<std::string>("oh no")};
auto val1_trans = std::move(val1).transpose();
auto val2_trans = std::move(val2).transpose();
auto val3_trans = std::move(val3).transpose();
assert(val1_trans.is_ok());
assert(val1_trans->is_none());
assert(val2_trans.is_ok());
assert(val2_trans->is_some());
assert(**val2_trans == 42);
assert(val3_trans.is_error());
assert(val3_trans.error() == "oh no");

◆ transpose() [2/2]

constexpr auto transpose ( ) const &
inlineconstexpr

Converts option<result<T, E>> into result<option<T>, E>

If the option is none, a result containing a none option is returned. If the option contains an ok result, a result containing an ok value of an option with a value is returned. If the option contains a result with an error value, a result with an error value is returned.

This function only participates in overload resolution if T is a result.

Example

option<result<int, std::string>> val1{};
option<result<int, std::string>> val2{ok<int>(42)};
option<result<int, std::string>> val3{error<std::string>("oh no")};
auto val1_trans = val1.transpose();
auto val2_trans = val2.transpose();
auto val3_trans = val3.transpose();
assert(val1_trans.is_ok());
assert(val1_trans->is_none());
assert(val2_trans.is_ok());
assert(val2_trans->is_some());
assert(**val2_trans == 42);
assert(val3_trans.is_error());
assert(val3_trans.error() == "oh no");

◆ value() [1/4]

constexpr reference value ( ) &
inlineconstexpr

Accesses the value contained in the option.

This function first checks if the option contains a value before attempting to access the value. If the option is none, then this function throws an exception.

Example

option<int> opt{42};
assert(opt.value() == 42);
Exceptions
bad_option_accessThrown if the option is none.

◆ value() [2/4]

constexpr rvalue_reference value ( ) &&
inlineconstexpr

Accesses the value contained in the option.

This function first checks if the option contains a value before attempting to access the value. If the option is none, then this function throws an exception.

Example

option<int> opt{42};
assert(std::move(opt).value() == 42);
Exceptions
bad_option_accessThrown if the option is none.

◆ value() [3/4]

constexpr const_reference value ( ) const &
inlineconstexpr

Accesses the value contained in the option.

This function first checks if the option contains a value before attempting to access the value. If the option is none, then this function throws an exception.

Example

const option<int> opt{42};
assert(opt.value() == 42);
Exceptions
bad_option_accessThrown if the option is none.

◆ value() [4/4]

constexpr rvalue_reference value ( ) const &&
inlineconstexpr

Accesses the value contained in the option.

This function first checks if the option contains a value before attempting to access the value. If the option is none, then this function throws an exception.

Example

const option<int> opt{42};
assert(std::move(opt).value() == 42);
Exceptions
bad_option_accessThrown if the option is none.

◆ value_or() [1/2]

constexpr value_type value_or ( U &&  default_value) &&
inlineconstexpr

Gets the option value with a default used for none.

If the option contains a value, the moved value is returned. If the option is none, the provided default value is returned as static_cast<T>(std::forward<U>(default_value)).

Example

option<int> opt1{};
option<int> opt2{42};
assert(std::move(opt1).value_or(0) == 0);
assert(std::move(opt2).value_or(0) == 42);
constexpr value_type value_or(U &&default_value) const &
Gets the option value with a default used for none.
Definition: option.hpp:1166
Parameters
default_valueThe value to use if the option is none.

◆ value_or() [2/2]

constexpr value_type value_or ( U &&  default_value) const &
inlineconstexpr

Gets the option value with a default used for none.

If the option contains a value, a copy of the value is returned. If the option is none, the provided default value is returned as static_cast<T>(std::forward<U>(default_value)).

Example

option<int> opt1{};
option<int> opt2{42};
assert(opt1.value_or(0) == 0);
assert(opt2.value_or(0) == 42);
Parameters
default_valueThe value to use if the option is none.

◆ value_or_else() [1/2]

constexpr value_type value_or_else ( F &&  f) &&
inlineconstexpr

Gets the option value with a default used for none.

If the option contains a value, the moved value is returned. If the option is none, the result of invoking f is returned. The returned value of f is also explicitly cast to value_type before return.

This function might be preferred over .value_or(default_value) when the default value is expensive to create or move. This function allows the default value to never be instantiated if the option is not none.

Example

option<int> opt1{};
option<int> opt2{42};
assert(std::move(opt1).value_or_else([] { return 0; }) == 0);
assert(std::move(opt2).value_or_else([] { return 0; }) == 42);
constexpr value_type value_or_else(F &&f) const &
Gets the option value with a default used for none.
Definition: option.hpp:1226
Parameters
fA callable that creates a default value.

◆ value_or_else() [2/2]

constexpr value_type value_or_else ( F &&  f) const &
inlineconstexpr

Gets the option value with a default used for none.

If the option contains a value, the copied value is returned. If the option is none, the result of invoking f is returned. The returned value of f is also explicitly cast to value_type before return.

This function might be preferred over .value_or(default_value) when the default value is expensive to create or move. This function allows the default value to never be instantiated if the option is not none.

Example

option<int> opt1{};
option<int> opt2{42};
assert(opt1.value_or_else([] { return 0; }) == 0);
assert(opt2.value_or_else([] { return 0; }) == 42);
Parameters
fA callable that creates a default value.

◆ visit() [1/4]

constexpr DEDUCED visit ( V &&  visitor) &
inlineconstexpr

Calls a visitor callable with the contained value or void.

This function treats an option as if it was a variant of void and T. If the option contains a value, that value is passed to the visitor. If the option is none, the visitor is called with void_t.

Note that the overload function can be helpful for defining a visitor inline.

Example

option<int> opt1{};
option<int> opt2{42};
opt1.visit(overload([](int value) {
assert(false);
}, [](void_t value) {
assert(true);
});
opt2.visit(overload([](int value) {
assert(value == 42);
}, [](void_t value) {
assert(false);
});

◆ visit() [2/4]

constexpr DEDUCED visit ( V &&  visitor) &&
inlineconstexpr

Calls a visitor callable with the contained value or void.

This function treats an option as if it was a variant of void and T. If the option contains a value, that value is passed to the visitor. If the option is none, the visitor is called without any arguments.

Note that the overload function can be helpful for defining a visitor inline.

Example

option<int> opt1{};
option<int> opt2{42};
std::move(opt1).visit(overload([](int value) {
assert(false);
}, [](void_t value) {
assert(true);
});
std::move(opt2).visit(overload([](int value) {
assert(value == 42);
}, [](void_t value) {
assert(false);
});

◆ visit() [3/4]

constexpr DEDUCED visit ( V &&  visitor) const &
inlineconstexpr

Calls a visitor callable with the contained value or void.

This function treats an option as if it was a variant of void and T. If the option contains a value, that value is passed to the visitor. If the option is none, the visitor is called with void_t.

Note that the overload function can be helpful for defining a visitor inline.

Example

const option<int> opt1{};
const option<int> opt2{42};
opt1.visit(overload([](int value) {
assert(false);
}, [](void_t value) {
assert(true);
});
opt2.visit(overload([](int value) {
assert(value == 42);
}, [](void_t value) {
assert(false);
});

◆ visit() [4/4]

constexpr DEDUCED visit ( V &&  visitor) const &&
inlineconstexpr

Calls a visitor callable with the contained value or void.

This function treats an option as if it was a variant of void and T. If the option contains a value, that value is passed to the visitor. If the option is none, the visitor is called with void_t.

Note that the overload function can be helpful for defining a visitor inline.

Example

const option<int> opt1{};
const option<int> opt2{42};
std::move(opt1).visit(overload([](int value) {
assert(false);
}, [](void_t value) {
assert(true);
});
std::move(opt2).visit(overload([](int value) {
assert(value == 42);
}, [](void_t value) {
assert(false);
});

◆ visit_informed() [1/4]

constexpr DEDUCED visit_informed ( V &&  visitor) &
inlineconstexpr

Calls a visitor callable with the contained value and meta data.

This function treats an option as if it was a variant of void and T. If the option contains a value, that value is passed to the visitor. If the option is none, the visitor is called with void_t.

In either case, an additional (second) parameter is passed to the visitor, which provides constexpr information about the specific alternative being visited. This info parameter has the API shown below. Note that this type is always an empty type.

struct alternative_info {
// index of the alternative in the source variant
static inline constexpr size_t index = ...;
// type of the alternative as declared in the source variant
using type = ...;
// helper function for forwarding non-const alternative values
// without needing to provide a template argument.
static constexpr decltype(auto) forward(...);
};
constexpr size_t index() const noexcept
Gets the index of the contained alternative.
Definition: variant.hpp:825

Note that the overload function can be helpful for defining a visitor inline.

Example

option<int> opt1{};
option<int> opt2{42};
opt1.visit_informed([](auto value, auto info) {
if constexpr (info.index == 0) { // none
assert(true);
} else if constexpr (info.index == 1) { // some
assert(false);
}
});
opt1.visit_informed([](auto value, auto info) {
if constexpr (info.index == 0) { // none
assert(false);
} else if constexpr (info.index == 1) { // some
assert(value == 42);
}
});

◆ visit_informed() [2/4]

constexpr DEDUCED visit_informed ( V &&  visitor) &&
inlineconstexpr

Calls a visitor callable with the contained value and meta data.

This function treats an option as if it was a variant of void and T. If the option contains a value, that value is passed to the visitor. If the option is none, the visitor is called with void_t.

In either case, an additional (second) parameter is passed to the visitor, which provides constexpr information about the specific alternative being visited. This info parameter has the API shown below. Note that this type is always an empty type.

struct alternative_info {
// index of the alternative in the source variant
static inline constexpr size_t index = ...;
// type of the alternative as declared in the source variant
using type = ...;
// helper function for forwarding non-const alternative values
// without needing to provide a template argument.
static constexpr decltype(auto) forward(...);
};

Note that the overload function can be helpful for defining a visitor inline.

Example

option<int> opt1{};
option<int> opt2{42};
std::move(opt1).visit_informed([](auto value, auto info) {
if constexpr (info.index == 0) { // none
assert(true);
} else if constexpr (info.index == 1) { // some
assert(false);
}
});
std::move(opt1).visit_informed([](auto value, auto info) {
if constexpr (info.index == 0) { // none
assert(false);
} else if constexpr (info.index == 1) { // some
assert(value == 42);
}
});

◆ visit_informed() [3/4]

constexpr DEDUCED visit_informed ( V &&  visitor) const &
inlineconstexpr

Calls a visitor callable with the contained value and meta data.

This function treats an option as if it was a variant of void and T. If the option contains a value, that value is passed to the visitor. If the option is none, the visitor is called with void_t.

In either case, an additional (second) parameter is passed to the visitor, which provides constexpr information about the specific alternative being visited. This info parameter has the API shown below. Note that this type is always an empty type.

struct alternative_info {
// index of the alternative in the source variant
static inline constexpr size_t index = ...;
// type of the alternative as declared in the source variant
using type = ...;
// helper function for forwarding non-const alternative values
// without needing to provide a template argument.
static constexpr decltype(auto) forward(...);
};

Note that the overload function can be helpful for defining a visitor inline.

Example

const option<int> opt1{};
const option<int> opt2{42};
opt1.visit_informed([](auto value, auto info) {
if constexpr (info.index == 0) { // none
assert(true);
} else if constexpr (info.index == 1) { // some
assert(false);
}
});
opt1.visit_informed([](auto value, auto info) {
if constexpr (info.index == 0) { // none
assert(false);
} else if constexpr (info.index == 1) { // some
assert(value == 42);
}
});

◆ visit_informed() [4/4]

constexpr DEDUCED visit_informed ( V &&  visitor) const &&
inlineconstexpr

Calls a visitor callable with the contained value and meta data.

This function treats an option as if it was a variant of void and T. If the option contains a value, that value is passed to the visitor. If the option is none, the visitor is called with void_t.

In either case, an additional (second) parameter is passed to the visitor, which provides constexpr information about the specific alternative being visited. This info parameter has the API shown below. Note that this type is always an empty type.

struct alternative_info {
// index of the alternative in the source variant
static inline constexpr size_t index = ...;
// type of the alternative as declared in the source variant
using type = ...;
// helper function for forwarding non-const alternative values
// without needing to provide a template argument.
static constexpr decltype(auto) forward(...);
};

Note that the overload function can be helpful for defining a visitor inline.

Example

const option<int> opt1{};
const option<int> opt2{42};
std::move(opt1).visit_informed([](auto value, auto info) {
if constexpr (info.index == 0) { // none
assert(true);
} else if constexpr (info.index == 1) { // some
assert(false);
}
});
std::move(opt1).visit_informed([](auto value, auto info) {
if constexpr (info.index == 0) { // none
assert(false);
} else if constexpr (info.index == 1) { // some
assert(value == 42);
}
});

Friends And Related Function Documentation

◆ get() [1/8]

constexpr CONST_RVALUE_REFERENCE get ( const option< T > &&  opt)
related

Gets an option value by index, as if it were a variant.

This function is provided to make option generically compatible with variant. This function treats option<T> as if it were variant<void, T>, where index 0 gets void (which represents none) and index 1 gets T (which is the contained value).

Example

const option<int> opt{42};
assert(get<1>(std::move(opt)) == 42);
Template Parameters
IDXThe "alternative" index
Parameters
optThe option to access
Returns
A const rvalue reference to the accessed "alternative"
Exceptions
bad_option_accessThrown if the option state does not match the index IDX.

◆ get() [2/8]

constexpr CONST_REFERENCE get ( const option< T > &  opt)
related

Gets an option value by index, as if it were a variant.

This function is provided to make option generically compatible with variant. This function treats option<T> as if it were variant<void, T>, where index 0 gets void (which represents none) and index 1 gets T (which is the contained value).

Example

const option<int> opt{42};
assert(get<1>(opt) == 42);
Template Parameters
IDXThe "alternative" index
Parameters
optThe option to access
Returns
A const reference to the accessed "alternative"
Exceptions
bad_option_accessThrown if the option state does not match the index IDX.

◆ get() [3/8]

constexpr CONST_RVALUE_REFERENCE get ( const option< U > &&  opt)
related

Gets an option value by type, as if it were a variant.

This function is provided to make option generically compatible with variant. This function treats option<T> as if it were variant<void, T>, where void is the none alternative, and T is the alternative with a value.

This function only participates in overload resolution if the contained value of the option is not void.

Example

const option<int> opt{42};
assert(get<int>(std::move(opt)) == 42);
Template Parameters
TThe type of the alternative to access
Parameters
optThe option to access
Returns
A const rvalue reference to the accessed alternative
Exceptions
bad_option_accessThrown if the option does not contain the requested type T.

◆ get() [4/8]

constexpr CONST_REFERENCE get ( const option< U > &  opt)
related

Gets an option value by type, as if it were a variant.

This function is provided to make option generically compatible with variant. This function treats option<T> as if it were variant<void, T>, where void is the none alternative, and T is the alternative with a value.

This function only participates in overload resolution if the contained value of the option is not void.

Example

const option<int> opt{42};
assert(get<int>(opt) == 42);
Template Parameters
TThe type of the alternative to access
Parameters
optThe option to access
Returns
A const reference to the accessed alternative
Exceptions
bad_option_accessThrown if the option does not contain the requested type T.

◆ get() [5/8]

constexpr RVALUE_REFERENCE get ( option< T > &&  opt)
related

Gets an option value by index, as if it were a variant.

This function is provided to make option generically compatible with variant. This function treats option<T> as if it were variant<void, T>, where index 0 gets void (which represents none) and index 1 gets T (which is the contained value).

Example

option<int> opt{42};
assert(get<1>(std::move(opt)) == 42);
Template Parameters
IDXThe "alternative" index
Parameters
optThe option to access
Returns
An rvalue reference to the accessed "alternative"
Exceptions
bad_option_accessThrown if the option state does not match the index IDX.

◆ get() [6/8]

constexpr REFERENCE get ( option< T > &  opt)
related

Gets an option value by index, as if it were a variant.

This function is provided to make option generically compatible with variant. This function treats option<T> as if it were variant<void, T>, where index 0 gets void (which represents none) and index 1 gets T (which is the contained value).

Example

option<int> opt{42};
assert(get<1>(opt) == 42);
Template Parameters
IDXThe "alternative" index
Parameters
optThe option to access
Returns
A reference to the accessed "alternative"
Exceptions
bad_option_accessThrown if the option state does not match the index IDX.

◆ get() [7/8]

constexpr RVALUE_REFERENCE get ( option< U > &&  opt)
related

Gets an option value by type, as if it were a variant.

This function is provided to make option generically compatible with variant. This function treats option<T> as if it were variant<void, T>, where void is the none alternative, and T is the alternative with a value.

This function only participates in overload resolution if the contained value of the option is not void.

Example

option<int> opt{42};
assert(get<int>(std::move(opt)) == 42);
Template Parameters
TThe type of the alternative to access
Parameters
optThe option to access
Returns
An rvalue reference to the accessed alternative
Exceptions
bad_option_accessThrown if the option does not contain the requested type T.

◆ get() [8/8]

constexpr REFERENCE get ( option< U > &  opt)
related

Gets an option value by type, as if it were a variant.

This function is provided to make option generically compatible with variant. This function treats option<T> as if it were variant<void, T>, where void is the none alternative, and T is the alternative with a value.

This function only participates in overload resolution if the contained value of the option is not void.

Example

option<int> opt{42};
assert(get<int>(opt) == 42);
Template Parameters
TThe type of the alternative to access
Parameters
optThe option to access
Returns
A reference to the accessed alternative
Exceptions
bad_option_accessThrown if the option does not contain the requested type T.

◆ operator() [1/6]

constexpr auto operator<=> ( const option< T > &  lhs,
const option< U > &  rhs 
)
related

Compares two option instances.

none is always less than any contained value.

Example

option<int> opt1{};
option<int> opt2{42};
option<int> opt3{24};
assert((opt1 <=> opt2) == std::strong_ordering::less);
assert((opt1 <=> opt3) == std::strong_ordering::less);
assert((opt2 <=> opt3) == std::strong_ordering::greater);
assert((opt2 <=> opt2) == std::strong_ordering::equal);

◆ operator() [2/6]

constexpr auto operator<=> ( const option< T > &  lhs,
const U &  rhs 
)
related

Compares an option with a value.

none is always less than the value. If not none, the contained value is compared with the value.

Example

option<int> opt1{};
option<int> opt2{42};
assert((opt1 <=> 42) == std::strong_ordering::less);
assert((42 <=> opt1) == std::strong_ordering::greater);
assert((opt2 <=> 42) == std::strong_ordering::equal);
assert((40 <=> opt2) == std::strong_ordering::less);

◆ operator() [3/6]

constexpr auto operator<=> ( const option< T > &  lhs,
const U *  rhs 
)
related

Compares an option with a pointer.

An option of an lvalue reference is a smart pointer and is directly comparable with raw pointers, where none is null, and a contained value is a non-null address.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int vals[] = {1, 2, 3, 4, 5};
option<int&> opt1{};
option<int&> opt2{vals[2]};
assert((opt1 <=> (vals + 2)) == std::strong_ordering::less);
assert(((vals + 2) <=> opt1) == std::strong_ordering::greater);
assert((opt2 <=> (vals + 1)) == std::strong_ordering::greater);
assert((opt2 <=> (vals + 2)) == std::strong_ordering::equal);
assert((opt2 <=> (vals + 3)) == std::strong_ordering::less);

◆ operator() [4/6]

constexpr std::strong_ordering operator<=> ( const option< T > &  lhs,
[[maybe_unused] ] none_t  rhs 
)
related

Compares an option with none.

none is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert((opt1 <=> none) == std::strong_ordering::equal);
assert((none <=> opt1) == std::strong_ordering::equal);
assert((opt2 <=> none) == std::strong_ordering::greater);
assert((none <=> opt2) == std::strong_ordering::less);

◆ operator() [5/6]

constexpr std::strong_ordering operator<=> ( const option< T > &  lhs,
[[maybe_unused] ] std::nullopt_t  rhs 
)
related

Compares an option with std::nullopt.

std::nullopt is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert((opt1 <=> std::nullopt) == std::strong_ordering::equal);
assert((std::nullopt <=> opt1) == std::strong_ordering::equal);
assert((opt2 <=> std::nullopt) == std::strong_ordering::greater);
assert((std::nullopt <=> opt2) == std::strong_ordering::less);

◆ operator() [6/6]

constexpr std::strong_ordering operator<=> ( const option< T > &  lhs,
[[maybe_unused] ] std::nullptr_t  rhs 
)
related

Compares an option with nullptr.

nullptr is always less than an option with a contained value.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int value = 42;
option<int&> opt1{};
option<int&> opt2{value};
assert((opt1 <=> nullptr) == std::strong_ordering::equal);
assert((nullptr <=> opt1) == std::strong_ordering::equal);
assert((opt2 <=> nullptr) == std::strong_ordering::greater);
assert((nullptr <=> opt2) == std::strong_ordering::less);

◆ operator!=() [1/11]

constexpr bool operator!= ( [[maybe_unused] ] none_t  lhs,
const option< T > &  rhs 
)
related

Compares an option with none.

none is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(!(none != opt1));
assert(none != opt2);

◆ operator!=() [2/11]

constexpr bool operator!= ( [[maybe_unused] ] std::nullopt_t  lhs,
const option< T > &  rhs 
)
related

Compares an option with std::nullopt.

std::nullopt is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(!(std::nullopt != opt1));
assert(std::nullopt != opt2);

◆ operator!=() [3/11]

constexpr bool operator!= ( [[maybe_unused] ] std::nullptr_t  lhs,
const option< T > &  rhs 
)
related

Compares an option with nullptr.

nullptr is always less than an option with a contained value.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int value = 42;
option<int&> opt1{};
option<int&> opt2{value};
assert(!(nullptr != opt1));
assert(nullptr != opt2);

◆ operator!=() [4/11]

constexpr bool operator!= ( const option< T > &  lhs,
const option< U > &  rhs 
)
related

Tests two option instances for inequality.

Example

option<int> opt1{};
option<int> opt2{42};
option<int> opt3{24};
assert(!(opt1 != opt1));
assert(!(opt2 != opt2));
assert(opt1 != opt2);
assert(opt2 != opt3);

◆ operator!=() [5/11]

constexpr bool operator!= ( const option< T > &  lhs,
const U &  rhs 
)
related

Compares an option with a value.

none is always less than the value. If not none, the contained value is compared with the value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(opt1 != 42);
assert(!(opt2 != 42));
assert(opt2 != 24);

◆ operator!=() [6/11]

constexpr bool operator!= ( const option< T > &  lhs,
const U *  rhs 
)
related

Compares an option with a pointer.

An option of an lvalue reference is a smart pointer and is directly comparable with raw pointers, where none is null, and a contained value is a non-null address.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int vals[] = {1, 2, 3, 4, 5};
option<int&> opt1{};
option<int&> opt2{vals[2]};
assert(opt1 != (vals + 2));
assert(!(opt2 != (vals + 2)));

◆ operator!=() [7/11]

constexpr bool operator!= ( const option< T > &  lhs,
[[maybe_unused] ] none_t  rhs 
)
related

Compares an option with none.

none is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(!(opt1 != none));
assert(opt2 != none);

◆ operator!=() [8/11]

constexpr bool operator!= ( const option< T > &  lhs,
[[maybe_unused] ] std::nullopt_t  rhs 
)
related

Compares an option with std::nullopt.

std::nullopt is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(!(opt1 != std::nullopt));
assert(opt2 != std::nullopt);

◆ operator!=() [9/11]

constexpr bool operator!= ( const option< T > &  lhs,
[[maybe_unused] ] std::nullptr_t  rhs 
)
related

Compares an option with nullptr.

nullptr is always less than an option with a contained value.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int value = 42;
option<int&> opt1{};
option<int&> opt2{value};
assert(!(opt1 != nullptr));
assert(opt2 != nullptr);

◆ operator!=() [10/11]

constexpr bool operator!= ( const U &  lhs,
const option< T > &  rhs 
)
related

Compares an option with a value.

none is always less than the value. If not none, the contained value is compared with the value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(42 != opt1);
assert(!(42 != opt2));
assert(24 != opt2);

◆ operator!=() [11/11]

constexpr bool operator!= ( const U *  lhs,
const option< T > &  rhs 
)
related

Compares an option with a pointer.

An option of an lvalue reference is a smart pointer and is directly comparable with raw pointers, where none is null, and a contained value is a non-null address.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int vals[] = {1, 2, 3, 4, 5};
option<int&> opt1{};
option<int&> opt2{vals[2]};
assert((vals + 2) != opt1);
assert(!((vals + 2) != opt2));

◆ operator<() [1/11]

constexpr bool operator< ( [[maybe_unused] ] const option< T > &  lhs,
[[maybe_unused] ] none_t  rhs 
)
related

Compares an option with none.

none is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(!(opt1 < none));
assert(!(opt2 < none));

◆ operator<() [2/11]

constexpr bool operator< ( [[maybe_unused] ] const option< T > &  lhs,
[[maybe_unused] ] std::nullopt_t  rhs 
)
related

Compares an option with std::nullopt.

std::nullopt is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(!(opt1 < std::nullopt));
assert(!(opt2 < std::nullopt));

◆ operator<() [3/11]

constexpr bool operator< ( [[maybe_unused] ] const option< T > &  lhs,
[[maybe_unused] ] std::nullptr_t  rhs 
)
related

Compares an option with nullptr.

nullptr is always less than an option with a contained value.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int value = 42;
option<int&> opt1{};
option<int&> opt2{value};
assert(!(opt1 < nullptr));
assert(!(opt2 < nullptr));

◆ operator<() [4/11]

constexpr bool operator< ( [[maybe_unused] ] none_t  lhs,
const option< T > &  rhs 
)
related

Compares an option with none.

none is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(!(none < opt1));
assert(none < opt2);

◆ operator<() [5/11]

constexpr bool operator< ( [[maybe_unused] ] std::nullopt_t  lhs,
const option< T > &  rhs 
)
related

Compares an option with std::nullopt.

std::nullopt is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(!(std::nullopt < opt1));
assert(std::nullopt < opt2);

◆ operator<() [6/11]

constexpr bool operator< ( [[maybe_unused] ] std::nullptr_t  lhs,
const option< T > &  rhs 
)
related

Compares an option with nullptr.

nullptr is always less than an option with a contained value.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int value = 42;
option<int&> opt1{};
option<int&> opt2{value};
assert(!(nullptr < opt1));
assert(nullptr < opt2);

◆ operator<() [7/11]

constexpr bool operator< ( const option< T > &  lhs,
const option< U > &  rhs 
)
related

Compares two option instances.

none is always less than any contained value.

Example

option<int> opt1{};
option<int> opt2{42};
option<int> opt3{24};
assert(opt1 < opt2);
assert(opt1 < opt3);
assert(opt3 < opt2);

◆ operator<() [8/11]

constexpr bool operator< ( const option< T > &  lhs,
const U &  rhs 
)
related

Compares an option with a value.

none is always less than the value. If not none, the contained value is compared with the value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(opt1 < 42);
assert(!(opt2 < 42));
assert(opt2 < 100);

◆ operator<() [9/11]

constexpr bool operator< ( const option< T > &  lhs,
const U *  rhs 
)
related

Compares an option with a pointer.

An option of an lvalue reference is a smart pointer and is directly comparable with raw pointers, where none is null, and a contained value is a non-null address.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int vals[] = {1, 2, 3, 4, 5};
option<int&> opt1{};
option<int&> opt2{vals[2]};
assert(opt1 < (vals + 2));
assert(!(opt2 < (vals + 2)));
assert(opt2 < (vals + 3));

◆ operator<() [10/11]

constexpr bool operator< ( const U &  lhs,
const option< T > &  rhs 
)
related

Compares an option with a value.

none is always less than the value. If not none, the contained value is compared with the value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(!(42 < opt1));
assert(!(42 < opt2));
assert(40 < opt2);

◆ operator<() [11/11]

constexpr bool operator< ( const U *  lhs,
const option< T > &  rhs 
)
related

Compares an option with a pointer.

An option of an lvalue reference is a smart pointer and is directly comparable with raw pointers, where none is null, and a contained value is a non-null address.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int vals[] = {1, 2, 3, 4, 5};
option<int&> opt1{};
option<int&> opt2{vals[2]};
assert(!((vals + 2) < opt1));
assert((vals + 2) < opt2);
assert(!((vals + 3) < opt2));

◆ operator<=() [1/11]

constexpr bool operator<= ( [[maybe_unused] ] none_t  lhs,
[[maybe_unused] ] const option< T > &  rhs 
)
related

Compares an option with none.

none is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(none <= opt1);
assert(none <= opt2);

◆ operator<=() [2/11]

constexpr bool operator<= ( [[maybe_unused] ] std::nullopt_t  lhs,
[[maybe_unused] ] const option< T > &  rhs 
)
related

Compares an option with std::nullopt.

std::nullopt is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(std::nullopt <= opt1);
assert(std::nullopt <= opt2);

◆ operator<=() [3/11]

constexpr bool operator<= ( [[maybe_unused] ] std::nullptr_t  lhs,
[[maybe_unused] ] const option< T > &  rhs 
)
related

Compares an option with nullptr.

nullptr is always less than an option with a contained value.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int value = 42;
option<int&> opt1{};
option<int&> opt2{value};
assert(nullptr <= opt1);
assert(nullptr <= opt2);

◆ operator<=() [4/11]

constexpr bool operator<= ( const option< T > &  lhs,
const option< U > &  rhs 
)
related

Compares two option instances.

none is always less than any contained value.

Example

option<int> opt1{};
option<int> opt2{42};
option<int> opt3{24};
assert(opt1 <= opt2);
assert(opt1 <= opt3);
assert(opt3 <= opt2);
assert(opt2 <= opt2);

◆ operator<=() [5/11]

constexpr bool operator<= ( const option< T > &  lhs,
const U &  rhs 
)
related

Compares an option with a value.

none is always less than the value. If not none, the contained value is compared with the value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(opt1 <= 42);
assert(opt2 <= 42);
assert(!(opt2 <= 100));

◆ operator<=() [6/11]

constexpr bool operator<= ( const option< T > &  lhs,
const U *  rhs 
)
related

Compares an option with a pointer.

An option of an lvalue reference is a smart pointer and is directly comparable with raw pointers, where none is null, and a contained value is a non-null address.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int vals[] = {1, 2, 3, 4, 5};
option<int&> opt1{};
option<int&> opt2{vals[2]};
assert(opt1 <= (vals + 2));
assert(!(opt2 <= (vals + 1)));
assert(opt2 <= (vals + 2));

◆ operator<=() [7/11]

constexpr bool operator<= ( const option< T > &  lhs,
[[maybe_unused] ] none_t  rhs 
)
related

Compares an option with none.

none is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(opt1 <= none);
assert(!(opt2 <= none));

◆ operator<=() [8/11]

constexpr bool operator<= ( const option< T > &  lhs,
[[maybe_unused] ] std::nullopt_t  rhs 
)
related

Compares an option with std::nullopt.

std::nullopt is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(opt1 <= std::nullopt);
assert(!(opt2 <= std::nullopt));

◆ operator<=() [9/11]

constexpr bool operator<= ( const option< T > &  lhs,
[[maybe_unused] ] std::nullptr_t  rhs 
)
related

Compares an option with nullptr.

nullptr is always less than an option with a contained value.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int value = 42;
option<int&> opt1{};
option<int&> opt2{value};
assert(opt1 <= nullptr);
assert(!(opt2 <= nullptr));

◆ operator<=() [10/11]

constexpr bool operator<= ( const U &  lhs,
const option< T > &  rhs 
)
related

Compares an option with a value.

none is always less than the value. If not none, the contained value is compared with the value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(!(42 <= opt1));
assert(42 <= opt2);
assert(!(100 <= opt2));

◆ operator<=() [11/11]

constexpr bool operator<= ( const U *  lhs,
const option< T > &  rhs 
)
related

Compares an option with a pointer.

An option of an lvalue reference is a smart pointer and is directly comparable with raw pointers, where none is null, and a contained value is a non-null address.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int vals[] = {1, 2, 3, 4, 5};
option<int&> opt1{};
option<int&> opt2{vals[2]};
assert(!((vals + 2) <= opt1));
assert((vals + 2) <= opt2);
assert(!((vals + 3) <= opt2));

◆ operator==() [1/11]

constexpr bool operator== ( [[maybe_unused] ] none_t  lhs,
const option< T > &  rhs 
)
related

Compares an option with none.

none is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(none == opt1);
assert(!(none == opt2));

◆ operator==() [2/11]

constexpr bool operator== ( [[maybe_unused] ] std::nullopt_t  lhs,
const option< T > &  rhs 
)
related

Compares an option with std::nullopt.

std::nullopt is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(std::nullopt == opt1);
assert(!(std::nullopt == opt2));

◆ operator==() [3/11]

constexpr bool operator== ( [[maybe_unused] ] std::nullptr_t  lhs,
const option< T > &  rhs 
)
related

Compares an option with nullptr.

nullptr is always less than an option with a contained value.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int value = 42;
option<int&> opt1{};
option<int&> opt2{value};
assert(nullptr == opt1);
assert(!(nullptr == opt2));

◆ operator==() [4/11]

constexpr bool operator== ( const option< T > &  lhs,
const option< U > &  rhs 
)
related

Tests two option instances for equality.

Example

option<int> opt1{};
option<int> opt2{42};
option<int> opt3{24};
assert(opt1 == opt1);
assert(opt2 == opt2);
assert(!(opt1 == opt2));
assert(!(opt2 == opt3));

◆ operator==() [5/11]

constexpr bool operator== ( const option< T > &  lhs,
const U &  rhs 
)
related

Compares an option with a value.

none is always less than the value. If not none, the contained value is compared with the value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(!(opt1 == 42));
assert(opt2 == 42);
assert(!(opt2 == 24));

◆ operator==() [6/11]

constexpr bool operator== ( const option< T > &  lhs,
const U *  rhs 
)
related

Compares an option with a pointer.

An option of an lvalue reference is a smart pointer and is directly comparable with raw pointers, where none is null, and a contained value is a non-null address.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int vals[] = {1, 2, 3, 4, 5};
option<int&> opt1{};
option<int&> opt2{vals[2]};
assert(!(opt1 == (vals + 2)));
assert(opt2 == (vals + 2));

◆ operator==() [7/11]

constexpr bool operator== ( const option< T > &  lhs,
[[maybe_unused] ] none_t  rhs 
)
related

Compares an option with none.

none is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(opt1 == none);
assert(!(opt2 == none));

◆ operator==() [8/11]

constexpr bool operator== ( const option< T > &  lhs,
[[maybe_unused] ] std::nullopt_t  rhs 
)
related

Compares an option with std::nullopt.

std::nullopt is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(opt1 == std::nullopt);
assert(!(opt2 == std::nullopt));

◆ operator==() [9/11]

constexpr bool operator== ( const option< T > &  lhs,
[[maybe_unused] ] std::nullptr_t  rhs 
)
related

Compares an option with nullptr.

nullptr is always less than an option with a contained value.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int value = 42;
option<int&> opt1{};
option<int&> opt2{value};
assert(opt1 == nullptr);
assert(!(opt2 == nullptr));

◆ operator==() [10/11]

constexpr bool operator== ( const U &  lhs,
const option< T > &  rhs 
)
related

Compares an option with a value.

none is always less than the value. If not none, the contained value is compared with the value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(!(42 == opt1));
assert(42 == opt2);
assert(!(24 == opt2));

◆ operator==() [11/11]

constexpr bool operator== ( const U *  lhs,
const option< T > &  rhs 
)
related

Compares an option with a pointer.

An option of an lvalue reference is a smart pointer and is directly comparable with raw pointers, where none is null, and a contained value is a non-null address.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int vals[] = {1, 2, 3, 4, 5};
option<int&> opt1{};
option<int&> opt2{vals[2]};
assert(!((vals + 2) == opt1));
assert((vals + 2) == opt2);

◆ operator>() [1/11]

constexpr bool operator> ( [[maybe_unused] ] none_t  lhs,
[[maybe_unused] ] const option< T > &  rhs 
)
related

Compares an option with none.

none is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(!(none > opt1));
assert(!(none > opt2));

◆ operator>() [2/11]

constexpr bool operator> ( [[maybe_unused] ] std::nullopt_t  lhs,
[[maybe_unused] ] const option< T > &  rhs 
)
related

Compares an option with std::nullopt.

std::nullopt is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(!(std::nullopt > opt1));
assert(!(std::nullopt > opt2));

◆ operator>() [3/11]

constexpr bool operator> ( [[maybe_unused] ] std::nullptr_t  lhs,
[[maybe_unused] ] const option< T > &  rhs 
)
related

Compares an option with nullptr.

nullptr is always less than an option with a contained value.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int value = 42;
option<int&> opt1{};
option<int&> opt2{value};
assert(!(nullptr > opt1));
assert(!(nullptr > opt2));

◆ operator>() [4/11]

constexpr bool operator> ( const option< T > &  lhs,
const option< U > &  rhs 
)
related

Compares two option instances.

none is always less than any contained value.

Example

option<int> opt1{};
option<int> opt2{42};
option<int> opt3{24};
assert(opt2 > opt1);
assert(opt3 > opt1);
assert(opt2 > opt3);

◆ operator>() [5/11]

constexpr bool operator> ( const option< T > &  lhs,
const U &  rhs 
)
related

Compares an option with a value.

none is always less than the value. If not none, the contained value is compared with the value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(!(opt1 > 42));
assert(!(opt2 > 42));
assert(opt2 > 40);

◆ operator>() [6/11]

constexpr bool operator> ( const option< T > &  lhs,
const U *  rhs 
)
related

Compares an option with a pointer.

An option of an lvalue reference is a smart pointer and is directly comparable with raw pointers, where none is null, and a contained value is a non-null address.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int vals[] = {1, 2, 3, 4, 5};
option<int&> opt1{};
option<int&> opt2{vals[2]};
assert(!(opt1 > (vals + 2)));
assert(!(opt2 > (vals + 2)));
assert(opt2 > (vals + 1));

◆ operator>() [7/11]

constexpr bool operator> ( const option< T > &  lhs,
[[maybe_unused] ] none_t  rhs 
)
related

Compares an option with none.

none is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(!(opt1 > none));
assert(opt2 > none);

◆ operator>() [8/11]

constexpr bool operator> ( const option< T > &  lhs,
[[maybe_unused] ] std::nullopt_t  rhs 
)
related

Compares an option with std::nullopt.

std::nullopt is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(!(opt1 > std::nullopt));
assert(opt2 > std::nullopt);

◆ operator>() [9/11]

constexpr bool operator> ( const option< T > &  lhs,
[[maybe_unused] ] std::nullptr_t  rhs 
)
related

Compares an option with nullptr.

nullptr is always less than an option with a contained value.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int value = 42;
option<int&> opt1{};
option<int&> opt2{value};
assert(!(opt1 > nullptr));
assert(opt2 > nullptr);

◆ operator>() [10/11]

constexpr bool operator> ( const U &  lhs,
const option< T > &  rhs 
)
related

Compares an option with a value.

none is always less than the value. If not none, the contained value is compared with the value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(42 > opt);
assert(!(42 > opt2));
assert(100 > opt2);

◆ operator>() [11/11]

constexpr bool operator> ( const U *  lhs,
const option< T > &  rhs 
)
related

Compares an option with a pointer.

An option of an lvalue reference is a smart pointer and is directly comparable with raw pointers, where none is null, and a contained value is a non-null address.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int vals[] = {1, 2, 3, 4, 5};
option<int&> opt1{};
option<int&> opt2{vals[2]};
assert((vals + 2) > opt1);
assert((vals + 2) > opt2);
assert(!((vals + 1) > opt2));

◆ operator>=() [1/11]

constexpr bool operator>= ( [[maybe_unused] ] const option< T > &  lhs,
[[maybe_unused] ] none_t  rhs 
)
related

Compares an option with none.

none is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(opt1 >= none);
assert(opt2 >= none);

◆ operator>=() [2/11]

constexpr bool operator>= ( [[maybe_unused] ] const option< T > &  lhs,
[[maybe_unused] ] std::nullopt_t  rhs 
)
related

Compares an option with std::nullopt.

std::nullopt is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(opt1 >= std::nullopt);
assert(opt2 >= std::nullopt);

◆ operator>=() [3/11]

constexpr bool operator>= ( [[maybe_unused] ] const option< T > &  lhs,
[[maybe_unused] ] std::nullptr_t  rhs 
)
related

Compares an option with nullptr.

nullptr is always less than an option with a contained value.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int value = 42;
option<int&> opt1{};
option<int&> opt2{value};
assert(opt1 >= nullptr);
assert(opt2 >= nullptr);

◆ operator>=() [4/11]

constexpr bool operator>= ( [[maybe_unused] ] none_t  lhs,
const option< T > &  rhs 
)
related

Compares an option with none.

none is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(none >= opt1);
assert(!(none >= opt2));

◆ operator>=() [5/11]

constexpr bool operator>= ( [[maybe_unused] ] std::nullopt_t  lhs,
const option< T > &  rhs 
)
related

Compares an option with std::nullopt.

std::nullopt is always less than an option with a contained value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(std::nullopt >= opt1);
assert(!(std::nullopt >= opt2));

◆ operator>=() [6/11]

constexpr bool operator>= ( [[maybe_unused] ] std::nullptr_t  lhs,
const option< T > &  rhs 
)
related

Compares an option with nullptr.

nullptr is always less than an option with a contained value.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int value = 42;
option<int&> opt1{};
option<int&> opt2{value};
assert(nullptr >= opt1);
assert(!(nullptr >= opt2));

◆ operator>=() [7/11]

constexpr bool operator>= ( const option< T > &  lhs,
const option< U > &  rhs 
)
related

Compares two option instances.

none is always less than any contained value.

Example

option<int> opt1{};
option<int> opt2{42};
option<int> opt3{24};
assert(opt2 >= opt1);
assert(opt3 >= opt1);
assert(opt2 >= opt3);
assert(opt2 >= opt2);

◆ operator>=() [8/11]

constexpr bool operator>= ( const option< T > &  lhs,
const U &  rhs 
)
related

Compares an option with a value.

none is always less than the value. If not none, the contained value is compared with the value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(!(opt1 >= 42));
assert(opt2 >= 42);
assert(!(opt2 >= 100));

◆ operator>=() [9/11]

constexpr bool operator>= ( const option< T > &  lhs,
const U *  rhs 
)
related

Compares an option with a pointer.

An option of an lvalue reference is a smart pointer and is directly comparable with raw pointers, where none is null, and a contained value is a non-null address.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int vals[] = {1, 2, 3, 4, 5};
option<int&> opt1{};
option<int&> opt2{vals[2]};
assert(!(opt1 >= (vals + 2)));
assert(opt2 >= (vals + 2));
assert(!(opt2 >= (vals + 3)));

◆ operator>=() [10/11]

constexpr bool operator>= ( const U &  lhs,
const option< T > &  rhs 
)
related

Compares an option with a value.

none is always less than the value. If not none, the contained value is compared with the value.

Example

option<int> opt1{};
option<int> opt2{42};
assert(42 >= opt1);
assert(42 >= opt2);
assert(!(40 >= opt2));

◆ operator>=() [11/11]

constexpr bool operator>= ( const U *  lhs,
const option< T > &  rhs 
)
related

Compares an option with a pointer.

An option of an lvalue reference is a smart pointer and is directly comparable with raw pointers, where none is null, and a contained value is a non-null address.

This function only participates in overload resolution if the contained type of the option is an lvalue reference.

Example

int vals[] = {1, 2, 3, 4, 5};
option<int&> opt1{};
option<int&> opt2{vals[2]};
assert((vals + 2) >= opt1);
assert(!((vals + 1) >= opt2));
assert((vals + 2) >= opt2);

◆ some() [1/2]

constexpr option< T > some ( Args &&...  args)
related

Constructs an option that contains a value.

This function is the counterpart to none that creates an option that contains a value instead of being none. The contained value is constructed in place in the option, which is then RVO returned.

A call to this function must explicitly specify the type T that the option should contain, and the passed arguments are forwarded to the constructor of that type.

Example

int value = 42;
auto opt1 = some<void>();
auto opt2 = some<int>(value);
auto opt3 = some<int&>(value);
assert(opt1.has_value());
assert(opt2.has_value());
assert(*opt2 == 42);
assert(opt3.has_value());
assert(opt3 == &value);

◆ some() [2/2]

constexpr option< T > some ( std::initializer_list< U >  ilist,
Args &&...  args 
)
related

Constructs an option that contains a value.

This function is the counterpart to none that creates an option that contains a value instead of being none. The contained value is constructed in place in the option, which is then RVO returned.

A call to this function must explicitly specify the type T that the option should contain, and the passed arguments are forwarded to the constructor of that type.

Example

auto opt = some<std::vector<int>>({1, 2, 3, 4, 5});
assert(opt.has_value());
assert(opt->size() == 5);

◆ swap()

constexpr void swap ( option< T > &  a,
option< T > &  b 
)
related

Swaps two option instances.

If both option instances contain a value, the two contained values are swapped directly. Otherwise, if one option contains a value, that value is move constructed into the other option, and the old value is destroyed in place. If both option instances are none, nothing is done.

This function is noexcept if the contained value type is nothrow swappable.

Example

option<int> opt1{};
option<int> opt2{42};
swap(opt1, opt2);
assert(opt1.has_value());
assert(*opt1 == 42);
assert(!opt1.has_value());
constexpr void swap(option &other) CONDITIONALLY_NOEXCEPT
Swaps two option instances.
Definition: option.hpp:3656

The documentation for this class was generated from the following files: