Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
arene::base::optional< T > Class Template Reference

Implementation of std::optional for pre-C++17 compilers, and which replaces defined undefined behavior with ARENE_PRECONDITION guards. More...

Inheritance diagram for arene::base::optional< T >:
Inheritance graph

Public Types

using value_type = T
 The type stored in the optional.
 

Public Member Functions

constexpr optional () noexcept
 Default construct to an empty state.
 
template<typename... Args, constraints< std::enable_if_t< sizeof() !=0 > > = nullptr>
constexpr optional (in_place_t, Args &&... args) noexcept(noexcept(T(std::forward< Args >(args)...)))
 Construct from a set of arguments.
 
constexpr optional (nullopt_t) noexcept
 Explicitly construct to an empty state.
 
constexpr optional (optional &&) noexcept(//NOLINTNEXTLINE(hicpp-noexcept-move) std::is_nothrow_move_constructible< T >::value)=default
 Move constructor.
 
constexpr optional (optional const &) noexcept(std::is_nothrow_copy_constructible< T >::value)=default
 Copy constructor.
 
template<typename U, constraints< std::enable_if_t< std::is_constructible< T, U >::value >, std::enable_if_t<!std::is_constructible< T, optional< U > >::value >, std::enable_if_t<!std::is_constructible< T, optional< U > & >::value >, std::enable_if_t<!std::is_constructible< T, optional< U > const & >::value > > = nullptr>
constexpr optional (optional< U > &&other) noexcept(noexcept(T(std::declval< U && >())))
 Construct from another optional.
 
template<typename U, constraints< std::enable_if_t< std::is_constructible< T, U >::value >, std::enable_if_t<!std::is_constructible< T, optional< U > >::value >, std::enable_if_t<!std::is_constructible< T, optional< U > & >::value >, std::enable_if_t<!std::is_constructible< T, optional< U > const & >::value > > = nullptr>
constexpr optional (optional< U > const &other) noexcept(noexcept(T(std::declval< U const & >())))
 Construct from another optional.
 
template<typename U, constraints< std::enable_if_t<!std::is_same< optional, remove_cvref_t< U > >::value >, std::enable_if_t<!std::is_same< nullopt_t, remove_cvref_t< U > >::value >, std::enable_if_t< std::is_constructible< T, U && >::value > > = nullptr>
constexpr optional (U &&other) noexcept(std::is_nothrow_constructible< T, U && >::value)
 Construct from a value.
 
 ~optional ()=default
 Default destructor.
 
 ARENE_IGNORE_ALL ("-Wfloat-equal", "Can only suppress this here, not in user code; std::vector does the same thing")
 
 ARENE_IGNORE_ALL ("-Wsign-compare", "Can only suppress this here, not in user code; std::vector does the same thing")
 
 ARENE_IGNORE_END ()
 
 ARENE_IGNORE_START ()
 
template<typename... Args>
constexpr void emplace (Args &&... args) noexcept(noexcept(std::declval< optional< T > >().construct_from(std::forward< Args >(args)...)))
 Destroy the old value if there is one, and construct a new value.
 
constexpr operator bool () const noexcept
 Query if this object has a value.
 
constexpr auto operator= (nullopt_t) noexcept -> optional &
 Reset via assignment.
 
constexpr auto operator= (optional &&other) noexcept(std::is_nothrow_move_constructible< T >::value &&std::is_nothrow_move_assignable< T >::value) -> optional &
 Move assign.
 
constexpr auto operator= (optional const &other) noexcept(std::is_nothrow_copy_constructible< T >::value &&std::is_nothrow_copy_assignable< T >::value) -> optional &
 Copy assign.
 
template<typename U = T, constraints< std::enable_if_t<!std::is_same< remove_cvref_t< U >, optional >::value >, std::enable_if_t< std::is_constructible< T, U >::value >, std::enable_if_t< std::is_assignable< T, U >::value > > = nullptr>
auto operator= (U &&other) noexcept(std::is_nothrow_assignable< T, U >::value &&noexcept(std::declval< optional & >().construct_from(std::forward< U >(other)))) -> optional &
 Assign from a value.
 
template<typename U = T, constraints< std::enable_if_t< is_swappable_v< U > >, std::enable_if_t< std::is_move_constructible< U >::value > > = nullptr>
constexpr void swap (optional &other) noexcept(//is_nothrow_swappable_v< U > &&std::is_nothrow_move_constructible< U >::value)
 Exchanges the state of two optionals.
 
constexpr auto operator* () const &noexcept -> T const &
 Accesses the value in the optional.
 
constexpr auto operator* () &noexcept -> T &
 Accesses the value in the optional.
 
constexpr auto operator* () const &&noexcept -> T const &&
 Accesses the value in the optional.
 
constexpr auto operator* () &&noexcept -> T &&
 Accesses the value in the optional.
 
constexpr auto operator-> () const noexcept -> T const *
 Access a pointer to the value in the optional.
 
constexpr auto operator-> () noexcept -> T *
 Access a pointer to the value in the optional.
 
template<typename U, typename R = T, constraints< std::enable_if_t< std::is_copy_constructible< R >::value >, std::enable_if_t< std::is_convertible< U, R >::value > > = nullptr>
constexpr auto value_or (U &&default_value) const &noexcept(noexcept(optional::value_or(std::declval< optional const & >(), std::forward< U >(default_value)))) -> T
 Gets the value held in the optional, or else the provided default.
 
template<typename U, typename R = T, constraints< std::enable_if_t< std::is_move_constructible< R >::value >, std::enable_if_t< std::is_convertible< U, R >::value > > = nullptr>
constexpr auto value_or (U &&default_value) &&noexcept(noexcept(optional::value_or(std::declval< optional && >(), std::forward< U >(default_value)))) -> T
 Gets the value held in the optional, or else the provided default.
 
template<typename F, typename R = T, constraints< std::enable_if_t< std::is_copy_constructible< R >::value >, std::enable_if_t< std::is_convertible< invoke_result_t< F >, R >::value > > = nullptr>
constexpr auto value_or_else (F &&default_generator) const &noexcept(noexcept(optional::value_or_else(std::declval< optional const & >(), std::forward< F >(default_generator)))) -> T
 Gets the value held in the optional, or else invokes the provided callable to generate one.
 
template<typename F, typename R = T, constraints< std::enable_if_t< std::is_move_constructible< R >::value >, std::enable_if_t< std::is_convertible< invoke_result_t< F >, R >::value > > = nullptr>
constexpr auto value_or_else (F &&default_generator) &&noexcept(noexcept(optional::value_or_else(std::declval< optional && >(), std::forward< F >(default_generator)))) -> T
 Gets the value held in the optional, or else invokes the provided callable to generate one.
 
template<typename F>
constexpr auto and_then (F &&handle_value) &-> invoke_result_t< F, T & >
 Monadic API which invokes a functor with the contents of value() if has_value() is true.
 
template<typename F>
constexpr auto and_then (F &&handle_value) const &-> invoke_result_t< F, T const & >
 Monadic API which invokes a functor with the contents of value() if has_value() is true.
 
template<typename F>
constexpr auto and_then (F &&handle_value) &&-> invoke_result_t< F, T && >
 Monadic API which invokes a functor with the contents of value() if has_value() is true.
 
template<typename F>
constexpr auto and_then (F &&handle_value) const &&-> invoke_result_t< F, T const && >
 Monadic API which invokes a functor with the contents of value() if has_value() is true.
 
template<typename F, typename R = T, constraints< std::enable_if_t< is_invocable_v< F > >, std::enable_if_t< std::is_copy_constructible< R >::value > > = nullptr>
constexpr auto or_else (F &&handle_null) const &-> optional< T >
 Monadic API which invokes a functor if has_value() is false.
 
template<typename F, typename R = T, constraints< std::enable_if_t< is_invocable_v< F > >, std::enable_if_t< std::is_move_constructible< R >::value > > = nullptr>
constexpr auto or_else (F &&handle_null) &&-> optional< T >
 Monadic API which invokes a functor if has_value() is false.
 
template<typename F>
constexpr auto transform (F &&value_transformer) &-> optional< invoke_result_t< F, T & > >
 Calls a provided functor with value if it is populated, and produces a new optional with the return from it.
 
template<typename F>
constexpr auto transform (F &&value_transformer) const &-> optional< invoke_result_t< F, T const & > >
 Calls a provided functor with value if it is populated, and produces a new optional with the return from it.
 
template<typename F>
constexpr auto transform (F &&value_transformer) &&-> optional< invoke_result_t< F, T && > >
 Calls a provided functor with value if it is populated, and produces a new optional with the return from it.
 
template<typename F>
constexpr auto transform (F &&value_transformer) const &&-> optional< invoke_result_t< F, T const && > >
 Calls a provided functor with value if it is populated, and produces a new optional with the return from it.
 

Static Public Member Functions

template<typename U, constraints< std::enable_if_t<!has_fast_inequality_check_v< T, U > > > = nullptr>
static constexpr auto fast_inequality_check (optional const &lhs, optional< U > const &rhs) noexcept -> inequality_heuristic
 Fast inequality check between two optional instances, where there is not a fast inequality check between the two stored types.
 
template<typename U, constraints< std::enable_if_t< has_fast_inequality_check_v< T, U > > > = nullptr>
static constexpr auto fast_inequality_check (optional const &lhs, optional< U > const &rhs) noexcept -> inequality_heuristic
 Fast inequality check between two optional instances, where there is not a fast inequality check between the two stored types.
 
template<typename U, constraints< std::enable_if_t<!has_fast_inequality_check_v< T, U > >, std::enable_if_t<!std::is_same< U, nullopt_t >::value > > = nullptr>
static constexpr auto fast_inequality_check (optional const &lhs, U const &) noexcept -> inequality_heuristic
 Fast inequality check between an optional instance and a value, where there is not a fast inequality check between the two stored types.
 
template<typename U, constraints< std::enable_if_t< has_fast_inequality_check_v< T, U > >, std::enable_if_t<!std::is_same< U, nullopt_t >::value > > = nullptr>
static constexpr auto fast_inequality_check (optional const &lhs, U const &rhs) noexcept -> inequality_heuristic
 Fast inequality check between an optional instance and a value, where there is a fast inequality check between the stored type and the other value.
 
static constexpr auto three_way_compare (optional const &lhs, nullopt_t) noexcept -> strong_ordering
 Three-way comparison between an optional and nullopt_t.
 
template<typename Self, typename U, constraints< std::enable_if_t< std::is_same< Self, optional >::value >, std::enable_if_t< compare_three_way_supported_v< T, U > > > = nullptr>
static constexpr auto three_way_compare (Self const &lhs, optional< U > const &rhs) noexcept -> strong_ordering
 Three-way comparison between two optional instances.
 
template<typename Self, typename U, constraints< std::enable_if_t< std::is_same< Self, optional >::value >, std::enable_if_t<!optional_detail::is_optional< U >::value >, std::enable_if_t< compare_three_way_supported_v< T, U > > > = nullptr>
static constexpr auto three_way_compare (Self const &lhs, U const &rhs) noexcept -> strong_ordering
 Three-way comparison between an optional instance and a value.
 

Friends

template<typename U>
class optional
 
constexpr auto operator!= (nullopt_t, optional const &rhs) noexcept -> bool
 inequality comparison between an explicit nullopt and an optional instance
 
constexpr auto operator!= (optional const &lhs, nullopt_t) noexcept -> bool
 inequality comparison between an optional instance and an explicit nullopt
 
template<typename Self, typename U, constraints< std::enable_if_t< std::is_same< Self, optional >::value >, std::enable_if_t<!optional_detail::is_optional< U >::value >, std::enable_if_t< is_equality_comparable_v< T, U > > > = nullptr>
constexpr auto operator!= (Self const &lhs, optional< U > const &rhs) noexcept -> bool
 inequality comparison between two optional instances
 
constexpr auto operator== (optional const &lhs, nullopt_t) noexcept -> bool
 equality comparison between an optional instance and an explicit nullopt
 
template<typename Self, typename U, constraints< std::enable_if_t< std::is_same< Self, optional >::value >, std::enable_if_t<!optional_detail::is_optional< U >::value >, std::enable_if_t< is_equality_comparable_v< T, U > > > = nullptr>
constexpr auto operator== (Self const &lhs, optional< U > const &rhs) noexcept -> bool
 equality comparison between two optional instances
 
template<typename Self, typename U, constraints< std::enable_if_t< std::is_same< Self, optional >::value >, std::enable_if_t<!optional_detail::is_optional< U >::value >, std::enable_if_t< is_equality_comparable_v< T, U > > > = nullptr>
constexpr auto operator== (Self const &lhs, U const &rhs) noexcept -> bool
 equality comparison between an optional instance and a value
 
template<typename U = T, constraints< std::enable_if_t< is_swappable_v< U > >, std::enable_if_t< std::is_move_constructible< U >::value > > = nullptr>
constexpr void swap (optional &lhs, optional &rhs) noexcept(noexcept(lhs.swap(rhs)))
 swaps the elements between two optionals.
 

Detailed Description

template<typename T>
class arene::base::optional< T >

Implementation of std::optional for pre-C++17 compilers, and which replaces defined undefined behavior with ARENE_PRECONDITION guards.

Template Parameters
TThe type which should be held in the optional.

An optional is a sum-type which can be in one of two states:

  1. It is disengaged, which means it holds no value
  2. It is engaged, which means it holds a value of type T .

It is generally used as a return type from an API which is fallible, but for which failure is an expected operating condition and for which no additional context other than a simple boolean pass/fail is useful. It can also be used as a member variable in a class to allow for deferred initialization without needing dynamic allocation, or as an input parameter to a function where it is important to distinguish between "not provided" and "default value."

Member Typedef Documentation

◆ value_type

template<typename T>
using arene::base::optional< T >::value_type = T

The type stored in the optional.

Constructor & Destructor Documentation

◆ optional() [1/8]

template<typename T>
arene::base::optional< T >::optional ( )
inlineconstexprnoexcept

Default construct to an empty state.

◆ ~optional()

template<typename T>
arene::base::optional< T >::~optional ( )
default

Default destructor.

◆ optional() [2/8]

template<typename T>
arene::base::optional< T >::optional ( nullopt_t )
inlineconstexprnoexcept

Explicitly construct to an empty state.

◆ optional() [3/8]

template<typename T>
arene::base::optional< T >::optional ( optional< T > const & ) const
constexprdefaultnoexcept

Copy constructor.

◆ optional() [4/8]

template<typename T>
arene::base::optional< T >::optional ( optional< T > && ) const
constexprdefaultnoexcept

Move constructor.

◆ optional() [5/8]

template<typename T>
template<typename U, constraints< std::enable_if_t<!std::is_same< optional, remove_cvref_t< U > >::value >, std::enable_if_t<!std::is_same< nullopt_t, remove_cvref_t< U > >::value >, std::enable_if_t< std::is_constructible< T, U && >::value > > = nullptr>
arene::base::optional< T >::optional ( U && other)
inlineconstexprnoexcept

Construct from a value.

Template Parameters
UThe type of the value to construct from
Parameters
otherThe source to move from

◆ optional() [6/8]

template<typename T>
template<typename U, constraints< std::enable_if_t< std::is_constructible< T, U >::value >, std::enable_if_t<!std::is_constructible< T, optional< U > >::value >, std::enable_if_t<!std::is_constructible< T, optional< U > & >::value >, std::enable_if_t<!std::is_constructible< T, optional< U > const & >::value > > = nullptr>
arene::base::optional< T >::optional ( optional< U > && other)
inlineconstexprnoexcept

Construct from another optional.

Template Parameters
UThe type of the value to construct from
Parameters
otherThe source to move from

◆ optional() [7/8]

template<typename T>
template<typename U, constraints< std::enable_if_t< std::is_constructible< T, U >::value >, std::enable_if_t<!std::is_constructible< T, optional< U > >::value >, std::enable_if_t<!std::is_constructible< T, optional< U > & >::value >, std::enable_if_t<!std::is_constructible< T, optional< U > const & >::value > > = nullptr>
arene::base::optional< T >::optional ( optional< U > const & other)
inlineconstexprnoexcept

Construct from another optional.

Template Parameters
UThe type of the value to construct from
Parameters
otherThe source to copy from

◆ optional() [8/8]

template<typename T>
template<typename... Args, constraints< std::enable_if_t< sizeof() !=0 > > = nullptr>
arene::base::optional< T >::optional ( in_place_t ,
Args &&... args )
inlineconstexprnoexcept

Construct from a set of arguments.

Template Parameters
ArgsThe types of the arguments to construct the value from
Parameters
argsThe arguments to construct the value from

Member Function Documentation

◆ and_then() [1/4]

template<typename T>
template<typename F>
auto arene::base::optional< T >::and_then ( F && handle_value) && -> invoke_result_t<F, T&&>
inlineconstexpr

Monadic API which invokes a functor with the contents of value() if has_value() is true.

Template Parameters
FType of the visiting functor. Must be invocable with value_type matching the const/ref qualification of Self, and return any valid optional<U>.
Parameters
handle_valuethe functor to invoke if self.has_value() is true .
Returns
U where U is the return type of F and is an instantiation of optional , which will be:
  • The result of invoking handle_value with the contents of value() if has_value() is true.
  • A U instantiated with nullopt otherwise.

◆ and_then() [2/4]

template<typename T>
template<typename F>
auto arene::base::optional< T >::and_then ( F && handle_value) & -> invoke_result_t<F, T&>
inlineconstexpr

Monadic API which invokes a functor with the contents of value() if has_value() is true.

Template Parameters
FType of the visiting functor. Must be invocable with value_type matching the const/ref qualification of Self, and return any valid optional<U>.
Parameters
handle_valuethe functor to invoke if self.has_value() is true .
Returns
U where U is the return type of F and is an instantiation of optional , which will be:
  • The result of invoking handle_value with the contents of value() if has_value() is true.
  • A U instantiated with nullopt otherwise.

◆ and_then() [3/4]

template<typename T>
template<typename F>
auto arene::base::optional< T >::and_then ( F && handle_value) const && -> invoke_result_t<F, T const&&>
inlineconstexpr

Monadic API which invokes a functor with the contents of value() if has_value() is true.

Template Parameters
FType of the visiting functor. Must be invocable with value_type matching the const/ref qualification of Self, and return any valid optional<U>.
Parameters
handle_valuethe functor to invoke if self.has_value() is true .
Returns
U where U is the return type of F and is an instantiation of optional , which will be:
  • The result of invoking handle_value with the contents of value() if has_value() is true.
  • A U instantiated with nullopt otherwise.

◆ and_then() [4/4]

template<typename T>
template<typename F>
auto arene::base::optional< T >::and_then ( F && handle_value) const & -> invoke_result_t<F, T const&>
inlineconstexpr

Monadic API which invokes a functor with the contents of value() if has_value() is true.

Template Parameters
FType of the visiting functor. Must be invocable with value_type matching the const/ref qualification of Self, and return any valid optional<U>.
Parameters
handle_valuethe functor to invoke if self.has_value() is true .
Returns
U where U is the return type of F and is an instantiation of optional , which will be:
  • The result of invoking handle_value with the contents of value() if has_value() is true.
  • A U instantiated with nullopt otherwise.

◆ ARENE_IGNORE_ALL() [1/2]

template<typename T>
arene::base::optional< T >::ARENE_IGNORE_ALL ( "-Wfloat-equal" ,
"Can only suppress this here,
not in user code;std::vector does the same thing"  )

◆ ARENE_IGNORE_ALL() [2/2]

template<typename T>
arene::base::optional< T >::ARENE_IGNORE_ALL ( "-Wsign-compare" ,
"Can only suppress this here,
not in user code;std::vector does the same thing"  )

◆ ARENE_IGNORE_END()

template<typename T>
arene::base::optional< T >::ARENE_IGNORE_END ( )

◆ ARENE_IGNORE_START()

template<typename T>
arene::base::optional< T >::ARENE_IGNORE_START ( )

◆ emplace()

template<typename T>
template<typename... Args>
void arene::base::optional< T >::emplace ( Args &&... args)
inlineconstexprnoexcept

Destroy the old value if there is one, and construct a new value.

Template Parameters
ArgsThe types of the arguments for constructing the new value
Parameters
argsThe arguments for constructing the new value

◆ fast_inequality_check() [1/4]

template<typename T>
template<typename U, constraints< std::enable_if_t<!has_fast_inequality_check_v< T, U > > > = nullptr>
static constexpr auto arene::base::optional< T >::fast_inequality_check ( optional< T > const & lhs,
optional< U > const & rhs ) -> inequality_heuristic
inlinestaticconstexprnoexcept

Fast inequality check between two optional instances, where there is not a fast inequality check between the two stored types.

Template Parameters
UThe value type of the second optional
Parameters
lhsThe first object to compare
rhsThe first object to compare
Returns
inequality_heuristic inequality_heuristic::definitely_not_equal if lhs has a value and rhs does not, or vice versa, inequality_heuristic::may_or_may_not_be_equal otherwise

◆ fast_inequality_check() [2/4]

template<typename T>
template<typename U, constraints< std::enable_if_t< has_fast_inequality_check_v< T, U > > > = nullptr>
static constexpr auto arene::base::optional< T >::fast_inequality_check ( optional< T > const & lhs,
optional< U > const & rhs ) -> inequality_heuristic
inlinestaticconstexprnoexcept

Fast inequality check between two optional instances, where there is not a fast inequality check between the two stored types.

Template Parameters
UThe value type of the second optional
Parameters
lhsThe first object to compare
rhsThe first object to compare
Returns
inequality_heuristic inequality_heuristic::definitely_not_equal if lhs has a value and rhs does not, or vice versa, inequality_heuristic::may_or_may_not_be_equal if neither has a value, and delegeates to fast_inequality_check on the stored values otherwise

◆ fast_inequality_check() [3/4]

template<typename T>
template<typename U, constraints< std::enable_if_t<!has_fast_inequality_check_v< T, U > >, std::enable_if_t<!std::is_same< U, nullopt_t >::value > > = nullptr>
static constexpr auto arene::base::optional< T >::fast_inequality_check ( optional< T > const & lhs,
U const &  ) -> inequality_heuristic
inlinestaticconstexprnoexcept

Fast inequality check between an optional instance and a value, where there is not a fast inequality check between the two stored types.

Template Parameters
UThe value type of the second optional
Parameters
lhsThe first object to compare
Returns
inequality_heuristic inequality_heuristic::definitely_not_equal if lhs is empty, inequality_heuristic::may_or_may_not_be_equal otherwise

◆ fast_inequality_check() [4/4]

template<typename T>
template<typename U, constraints< std::enable_if_t< has_fast_inequality_check_v< T, U > >, std::enable_if_t<!std::is_same< U, nullopt_t >::value > > = nullptr>
static constexpr auto arene::base::optional< T >::fast_inequality_check ( optional< T > const & lhs,
U const & rhs ) -> inequality_heuristic
inlinestaticconstexprnoexcept

Fast inequality check between an optional instance and a value, where there is a fast inequality check between the stored type and the other value.

Template Parameters
UThe value type of the second optional
Parameters
lhsThe first object to compare
rhsThe first object to compare
Returns
inequality_heuristic inequality_heuristic::definitely_not_equal if lhs is empty, delegeates to fast_inequality_check on the stored values otherwise

◆ operator bool()

template<typename T>
arene::base::optional< T >::operator bool ( ) const
inlineexplicitconstexprnoexcept

Query if this object has a value.

Returns
bool Equivalent to has_value() .

◆ operator*() [1/4]

template<typename T>
auto arene::base::optional< T >::operator* ( ) && -> T&&
inlineconstexprnoexcept

Accesses the value in the optional.

Returns
A T which is cref-qualified the same as *this .
Precondition
has_value() , else ARENE_PRECONDITION violation.

◆ operator*() [2/4]

template<typename T>
auto arene::base::optional< T >::operator* ( ) & -> T&
inlineconstexprnoexcept

Accesses the value in the optional.

Returns
A T which is cref-qualified the same as *this .
Precondition
has_value() , else ARENE_PRECONDITION violation.

◆ operator*() [3/4]

template<typename T>
auto arene::base::optional< T >::operator* ( ) const && -> T const&&
inlineconstexprnoexcept

Accesses the value in the optional.

Returns
A T which is cref-qualified the same as *this .
Precondition
has_value() , else ARENE_PRECONDITION violation.

◆ operator*() [4/4]

template<typename T>
auto arene::base::optional< T >::operator* ( ) const & -> T const&
inlineconstexprnoexcept

Accesses the value in the optional.

Returns
A T which is cref-qualified the same as *this .
Precondition
has_value() , else ARENE_PRECONDITION violation.

◆ operator->() [1/2]

template<typename T>
auto arene::base::optional< T >::operator-> ( ) const -> T const*
inlineconstexprnoexcept

Access a pointer to the value in the optional.

Returns
A T* which is const-qualified the same as *this .
Precondition
has_value() , else ARENE_PRECONDITION violation.

◆ operator->() [2/2]

template<typename T>
auto arene::base::optional< T >::operator-> ( ) -> T*
inlineconstexprnoexcept

Access a pointer to the value in the optional.

Returns
A T* which is const-qualified the same as *this .
Precondition
has_value() , else ARENE_PRECONDITION violation.

◆ operator=() [1/4]

template<typename T>
auto arene::base::optional< T >::operator= ( nullopt_t ) -> optional&
inlineconstexprnoexcept

Reset via assignment.

◆ operator=() [2/4]

template<typename T>
auto arene::base::optional< T >::operator= ( optional< T > && other) -> optional&
inlineconstexprnoexcept

Move assign.

Parameters
otherThe value to assign from

◆ operator=() [3/4]

template<typename T>
auto arene::base::optional< T >::operator= ( optional< T > const & other) -> optional&
inlineconstexprnoexcept

Copy assign.

Parameters
otherThe value to assign from

◆ operator=() [4/4]

template<typename T>
template<typename U = T, constraints< std::enable_if_t<!std::is_same< remove_cvref_t< U >, optional >::value >, std::enable_if_t< std::is_constructible< T, U >::value >, std::enable_if_t< std::is_assignable< T, U >::value > > = nullptr>
auto arene::base::optional< T >::operator= ( U && other) -> optional&
inlinenoexcept

Assign from a value.

Parameters
otherThe value to assign from

◆ or_else() [1/2]

template<typename T>
template<typename F, typename R = T, constraints< std::enable_if_t< is_invocable_v< F > >, std::enable_if_t< std::is_move_constructible< R >::value > > = nullptr>
auto arene::base::optional< T >::or_else ( F && handle_null) && -> optional<T>
inlineconstexpr

Monadic API which invokes a functor if has_value() is false.

Template Parameters
FType of the visiting functor. Must be invocable with no arguments and return optional<T> .
Parameters
handle_nullthe functor to invoke if self.has_value() is false .
Returns
optional<T> which will be:
  • The result of invoking handle_value with the contents of value() if has_value() is false.
  • A instance of optional<T> constructed from forwarding self .

◆ or_else() [2/2]

template<typename T>
template<typename F, typename R = T, constraints< std::enable_if_t< is_invocable_v< F > >, std::enable_if_t< std::is_copy_constructible< R >::value > > = nullptr>
auto arene::base::optional< T >::or_else ( F && handle_null) const & -> optional<T>
inlineconstexpr

Monadic API which invokes a functor if has_value() is false.

Template Parameters
FType of the visiting functor. Must be invocable with no arguments and return optional<T> .
Parameters
handle_nullthe functor to invoke if self.has_value() is false .
Returns
optional<T> which will be:
  • The result of invoking handle_value with the contents of value() if has_value() is false.
  • A instance of optional<T> constructed from forwarding self .

◆ swap()

template<typename T>
template<typename U = T, constraints< std::enable_if_t< is_swappable_v< U > >, std::enable_if_t< std::is_move_constructible< U >::value > > = nullptr>
void arene::base::optional< T >::swap ( optional< T > & other)
inlineconstexprnoexcept

Exchanges the state of two optionals.

Template Parameters
UMust satisfy is_swappable_v and std::is_move_constructable.
Parameters
otherThe optional to swap with.
Postcondition
If both optionals are engaged, they will have their values exchanged as if via arene::base::swap . Otherwise if one of the optionals was nullopt , the states are exchanged as if by move-constructing the value from the engaged optional into the unengaged optional, and resetting the previously engaged optional.

◆ three_way_compare() [1/3]

template<typename T>
static constexpr auto arene::base::optional< T >::three_way_compare ( optional< T > const & lhs,
nullopt_t  ) -> strong_ordering
inlinestaticconstexprnoexcept

Three-way comparison between an optional and nullopt_t.

Parameters
lhsThe first object to compare
Returns
strong_ordering strong_ordering::equal if the lhs is empty, otherwise strong_ordering::greater

◆ three_way_compare() [2/3]

template<typename T>
template<typename Self, typename U, constraints< std::enable_if_t< std::is_same< Self, optional >::value >, std::enable_if_t< compare_three_way_supported_v< T, U > > > = nullptr>
static constexpr auto arene::base::optional< T >::three_way_compare ( Self const & lhs,
optional< U > const & rhs ) -> strong_ordering
inlinestaticconstexprnoexcept

Three-way comparison between two optional instances.

Template Parameters
SelfThe type of the optional
UThe value type of the second optional
Parameters
lhsThe first object to compare
rhsThe first object to compare
Returns
strong_ordering strong_ordering::less if lhs is empty and rhs has a value, strong_ordering::equal if both lhs and rhs are empty, strong_ordering::greater if lhs has a value and rhs is empty, and the result of three-way comparison between the stored values if both lhs and rhs have values
Precondition
T and U must be three-way-comparable

◆ three_way_compare() [3/3]

template<typename T>
template<typename Self, typename U, constraints< std::enable_if_t< std::is_same< Self, optional >::value >, std::enable_if_t<!optional_detail::is_optional< U >::value >, std::enable_if_t< compare_three_way_supported_v< T, U > > > = nullptr>
static constexpr auto arene::base::optional< T >::three_way_compare ( Self const & lhs,
U const & rhs ) -> strong_ordering
inlinestaticconstexprnoexcept

Three-way comparison between an optional instance and a value.

Template Parameters
SelfThe type of the optional
Uthe type of the value to compare against
Parameters
lhsThe first object to compare
rhsThe first object to compare
Returns
strong_ordering strong_ordering::less if lhs is empty, and the result of three-way comparison between the stored value of lhs and rhs otherwise
Precondition
T and U must be three-way-comparable

◆ transform() [1/4]

template<typename T>
template<typename F>
auto arene::base::optional< T >::transform ( F && value_transformer) && -> optional<invoke_result_t<F, T&&>>
inlineconstexpr

Calls a provided functor with value if it is populated, and produces a new optional with the return from it.

Template Parameters
FType of the visiting functor. Must be invocable with value_type matching the const/ref qualification of this. May return any valid value_type
Parameters
value_transformerthe functor to invoke if self.has_value() is true .
Returns
optional<U> where U is the return type of F , which will contain:
  • The result of invoking handle_value with the contents of value() if has_value() is true.
  • nullopt otherwise.

◆ transform() [2/4]

template<typename T>
template<typename F>
auto arene::base::optional< T >::transform ( F && value_transformer) & -> optional<invoke_result_t<F, T&>>
inlineconstexpr

Calls a provided functor with value if it is populated, and produces a new optional with the return from it.

Template Parameters
FType of the visiting functor. Must be invocable with value_type matching the const/ref qualification of this. May return any valid value_type
Parameters
value_transformerthe functor to invoke if self.has_value() is true .
Returns
optional<U> where U is the return type of F , which will contain:
  • The result of invoking handle_value with the contents of value() if has_value() is true.
  • nullopt otherwise.

◆ transform() [3/4]

template<typename T>
template<typename F>
auto arene::base::optional< T >::transform ( F && value_transformer) const && -> optional<invoke_result_t<F, T const&&>>
inlineconstexpr

Calls a provided functor with value if it is populated, and produces a new optional with the return from it.

Template Parameters
FType of the visiting functor. Must be invocable with value_type matching the const/ref qualification of this. May return any valid value_type
Parameters
value_transformerthe functor to invoke if self.has_value() is true .
Returns
optional<U> where U is the return type of F , which will contain:
  • The result of invoking handle_value with the contents of value() if has_value() is true.
  • nullopt otherwise.

◆ transform() [4/4]

template<typename T>
template<typename F>
auto arene::base::optional< T >::transform ( F && value_transformer) const & -> optional<invoke_result_t<F, T const&>>
inlineconstexpr

Calls a provided functor with value if it is populated, and produces a new optional with the return from it.

Template Parameters
FType of the visiting functor. Must be invocable with value_type matching the const/ref qualification of this. May return any valid value_type
Parameters
value_transformerthe functor to invoke if self.has_value() is true .
Returns
optional<U> where U is the return type of F , which will contain:
  • The result of invoking handle_value with the contents of value() if has_value() is true.
  • nullopt otherwise.

◆ value_or() [1/2]

template<typename T>
template<typename U, typename R = T, constraints< std::enable_if_t< std::is_move_constructible< R >::value >, std::enable_if_t< std::is_convertible< U, R >::value > > = nullptr>
auto arene::base::optional< T >::value_or ( U && default_value) && -> T
inlineconstexprnoexcept

Gets the value held in the optional, or else the provided default.

Template Parameters
UThe type of the default value. T must be constructible from U.
Parameters
default_valueThe value to return if the optional is null.
Returns
T constructed from the held value if has_value() is true , else an instance constructed from default_value .
Note
This must return a new instance rather than a reference, otherwise it risks dangling references if default_value is a temporary.

◆ value_or() [2/2]

template<typename T>
template<typename U, typename R = T, constraints< std::enable_if_t< std::is_copy_constructible< R >::value >, std::enable_if_t< std::is_convertible< U, R >::value > > = nullptr>
auto arene::base::optional< T >::value_or ( U && default_value) const & -> T
inlineconstexprnoexcept

Gets the value held in the optional, or else the provided default.

Template Parameters
UThe type of the default value. T must be constructible from U.
Parameters
default_valueThe value to return if the optional is null.
Returns
T constructed from the held value if has_value() is true , else an instance constructed from default_value .
Note
This must return a new instance rather than a reference, otherwise it risks dangling references if default_value is a temporary.

◆ value_or_else() [1/2]

template<typename T>
template<typename F, typename R = T, constraints< std::enable_if_t< std::is_move_constructible< R >::value >, std::enable_if_t< std::is_convertible< invoke_result_t< F >, R >::value > > = nullptr>
auto arene::base::optional< T >::value_or_else ( F && default_generator) && -> T
inlineconstexprnoexcept

Gets the value held in the optional, or else invokes the provided callable to generate one.

Template Parameters
FThe type of the callable to invoke. Must satisfy is_invocable_v<F> and std::is_convertible<invoke_result_t<F>,T> .
Parameters
default_generatorThe function to invoke to produce a default if !has_value() .
Returns
T constructed from the held value if has_value() is true , else an instance constructed from invoking default_generator .

◆ value_or_else() [2/2]

template<typename T>
template<typename F, typename R = T, constraints< std::enable_if_t< std::is_copy_constructible< R >::value >, std::enable_if_t< std::is_convertible< invoke_result_t< F >, R >::value > > = nullptr>
auto arene::base::optional< T >::value_or_else ( F && default_generator) const & -> T
inlineconstexprnoexcept

Gets the value held in the optional, or else invokes the provided callable to generate one.

Template Parameters
FThe type of the callable to invoke. Must satisfy is_invocable_v<F> and std::is_convertible<invoke_result_t<F>,T> .
Parameters
default_generatorThe function to invoke to produce a default if !has_value() .
Returns
T constructed from the held value if has_value() is true , else an instance constructed from invoking default_generator .

Friends And Related Symbol Documentation

◆ optional

template<typename T>
template<typename U>
friend class optional
friend

◆ operator!= [1/3]

template<typename T>
auto operator!= ( nullopt_t ,
optional< T > const & rhs ) -> bool
friend

inequality comparison between an explicit nullopt and an optional instance

Parameters
rhsThe optional instance to compare
Returns
bool true if rhs has a value (that is, has_value()), false if it does not

◆ operator!= [2/3]

template<typename T>
auto operator!= ( optional< T > const & lhs,
nullopt_t  ) -> bool
friend

inequality comparison between an optional instance and an explicit nullopt

Parameters
lhsThe optional instance to compare
Returns
bool true if lhs has a value (that is, has_value()), false if it does not

◆ operator!= [3/3]

template<typename T>
template<typename Self, typename U, constraints< std::enable_if_t< std::is_same< Self, optional >::value >, std::enable_if_t<!optional_detail::is_optional< U >::value >, std::enable_if_t< is_equality_comparable_v< T, U > > > = nullptr>
auto operator!= ( Self const & lhs,
optional< U > const & rhs ) -> bool
friend

inequality comparison between two optional instances

Template Parameters
SelfThe type of the optional
Uthe type stored in the other optional
Parameters
lhsThe first object to compare
rhsThe first object to compare
Returns
bool false if lhs is empty, and the result of an inequality comparison between the stored value of lhs and rhs otherwise
Precondition
T and U must be equality-comparable

◆ operator== [1/3]

template<typename T>
auto operator== ( optional< T > const & lhs,
nullopt_t  ) -> bool
friend

equality comparison between an optional instance and an explicit nullopt

Parameters
lhsThe optional instance to compare
Returns
bool true if lhs is null (that is, !has_value()), false if it has a value

◆ operator== [2/3]

template<typename T>
template<typename Self, typename U, constraints< std::enable_if_t< std::is_same< Self, optional >::value >, std::enable_if_t<!optional_detail::is_optional< U >::value >, std::enable_if_t< is_equality_comparable_v< T, U > > > = nullptr>
auto operator== ( Self const & lhs,
optional< U > const & rhs ) -> bool
friend

equality comparison between two optional instances

Template Parameters
SelfThe type of the optional
Uthe type stored in the other optional
Parameters
lhsThe first object to compare
rhsThe first object to compare
Returns
bool true if both lhs and rhs are empty, false if only one of lhs and rhs is empty, and the result of an equality comparison between the stored value of lhs and the stored value of rhs otherwise
Precondition
T and U must be equality-comparable

◆ operator== [3/3]

template<typename T>
template<typename Self, typename U, constraints< std::enable_if_t< std::is_same< Self, optional >::value >, std::enable_if_t<!optional_detail::is_optional< U >::value >, std::enable_if_t< is_equality_comparable_v< T, U > > > = nullptr>
auto operator== ( Self const & lhs,
U const & rhs ) -> bool
friend

equality comparison between an optional instance and a value

Template Parameters
SelfThe type of the optional
Uthe type of the value to compare against the optional
Parameters
lhsThe first object to compare
rhsThe first object to compare
Returns
bool false if lhs is empty, and the result of an equality comparison between the stored value of lhs and rhs otherwise
Precondition
T and U must be equality-comparable

◆ swap

template<typename T>
template<typename U = T, constraints< std::enable_if_t< is_swappable_v< U > >, std::enable_if_t< std::is_move_constructible< U >::value > > = nullptr>
void swap ( optional< T > & lhs,
optional< T > & rhs )
friend

swaps the elements between two optionals.

Template Parameters
Uthe type of the element in the optional. Must satisfy is_swappable_v .
Parameters
lhsthe left hand optional to swap.
rhsthe right hand optional to swap.
Postcondition
Equivalent to having called lhs.swap(rhs);
See also
optional::swap.

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