tetl 0.1.0
Embedded Template Library
|
The class template optional manages an optional contained value, i.e. a value that may or may not be present. More...
#include <etl/optional.hpp>
Public Types | |
using | const_iterator = T const * |
using | iterator = T * |
using | value_type = T |
Public Member Functions | |
constexpr | optional () noexcept=default |
Constructs an object that does not contain a value. | |
template<typename... Args> requires is_constructible_v<T, Args...> | |
constexpr | optional (in_place_t, Args &&... args) |
Constructs an optional object that contains a value, initialized as if direct-initializing. | |
constexpr | optional (nullopt_t) noexcept |
Constructs an object that does not contain a value. | |
constexpr | optional (optional &&) noexcept(is_nothrow_move_constructible_v< value_type >)=default |
Move constructor. | |
constexpr | optional (optional const &)=default |
Copy constructor. | |
template<typename F > | |
constexpr auto | and_then (F &&f) & |
template<typename F > | |
constexpr auto | and_then (F &&f) && |
template<typename F > | |
constexpr auto | and_then (F &&f) const & |
template<typename F > | |
constexpr auto | and_then (F &&f) const && |
constexpr auto | begin () const noexcept -> const_iterator |
constexpr auto | begin () noexcept -> iterator |
template<typename... Args> | |
constexpr auto | emplace (Args &&... args) -> value_type & |
Constructs the contained value in-place. If *this already contains a value before the call, the contained value is destroyed by calling its destructor. | |
constexpr auto | end () const noexcept -> const_iterator |
constexpr auto | end () noexcept -> iterator |
template<typename U > requires ( is_constructible_v<T, U &&> and not is_same_v<remove_cv_t<U>, bool> and not is_constructible_v<T, optional<U>&> and not is_constructible_v<T, optional<U> const&> and not is_constructible_v<T, optional<U> &&> and not is_constructible_v<T, optional<U> const &&> and not is_convertible_v<optional<U>&, T> and not is_convertible_v<optional<U> const&, T> and not is_convertible_v<optional<U> &&, T> and not is_convertible_v<optional<U> const &&, T> ) | |
explicit (not is_convertible_v< U &&, T >) const expr optional(optional< U > &&other) | |
Converting move constructor. | |
template<typename U = T> requires ( is_constructible_v<T, U &&> and not is_same_v<remove_cvref_t<U>, in_place_t> and not is_same_v<remove_cvref_t<U>, optional> ) | |
explicit (not is_convertible_v< U &&, T >) const expr optional(U &&value) | |
Constructs an optional object that contains a value, initialized as if direct-initializing. | |
template<typename U > requires ( is_constructible_v<T, U const&> and not is_same_v<remove_cv_t<U>, bool> and not is_constructible_v<T, optional<U>&> and not is_constructible_v<T, optional<U> const&> and not is_constructible_v<T, optional<U> &&> and not is_constructible_v<T, optional<U> const &&> and not is_convertible_v<optional<U>&, T> and not is_convertible_v<optional<U> const&, T> and not is_convertible_v<optional<U> &&, T> and not is_convertible_v<optional<U> const &&, T> ) | |
explicit (not is_convertible_v< U const &, T >) const expr optional(optional< U > const &other) | |
Converting copy constructor. | |
constexpr auto | has_value () const noexcept -> bool |
Checks whether *this contains a value. | |
constexpr | operator bool () const noexcept |
Checks whether *this contains a value. | |
constexpr auto | operator* () &&-> T && |
Returns a reference to the contained value. | |
constexpr auto | operator* () &-> T & |
Returns a reference to the contained value. | |
constexpr auto | operator* () const &&-> T const && |
Returns a reference to the contained value. | |
constexpr auto | operator* () const &-> T const & |
Returns a reference to the contained value. | |
constexpr auto | operator-> () -> value_type * |
Returns a pointer to the contained value. The pointer is null if the optional is empty. | |
constexpr auto | operator-> () const -> value_type const * |
Returns a pointer to the contained value. The pointer is null if the optional is empty. | |
constexpr auto | operator= (etl::nullopt_t) noexcept -> optional & |
If *this contains a value before the call, the contained value is destroyed by calling its destructor as if by value().T::~T(). *this does not contain a value after this call. | |
constexpr auto | operator= (optional &&other) noexcept -> optional &=default |
Assigns the state of other. | |
constexpr auto | operator= (optional const &other) -> optional &=default |
Assigns the state of other. | |
template<typename U = T> requires ( is_constructible_v<T, U> and is_assignable_v<T&, U> and not is_constructible_v<T, optional<U>&> and not is_constructible_v<T, optional<U> const&> and not is_constructible_v<T, optional<U> &&> and not is_constructible_v<T, optional<U> const &&> and not is_convertible_v<optional<U>&, T> and not is_convertible_v<optional<U> const&, T> and not is_convertible_v<optional<U> &&, T> and not is_convertible_v<optional<U> const &&, T> and not is_assignable_v<T&, optional<U>&> and not is_assignable_v<T&, optional<U> const&> and not is_assignable_v<T&, optional<U> &&> and not is_assignable_v<T&, optional<U> const &&> ) | |
constexpr auto | operator= (optional< U > &&other) -> optional & |
Assigns the state of other. | |
template<typename U = T> requires ( is_constructible_v<T, U const&> and is_assignable_v<T&, U const&> and not is_constructible_v<T, optional<U>&> and not is_constructible_v<T, optional<U> const&> and not is_constructible_v<T, optional<U> &&> and not is_constructible_v<T, optional<U> const &&> and not is_convertible_v<optional<U>&, T> and not is_convertible_v<optional<U> const&, T> and not is_convertible_v<optional<U> &&, T> and not is_convertible_v<optional<U> const &&, T> and not is_assignable_v<T&, optional<U>&> and not is_assignable_v<T&, optional<U> const&> and not is_assignable_v<T&, optional<U> &&> and not is_assignable_v<T&, optional<U> const &&> ) | |
constexpr auto | operator= (optional< U > const &other) -> optional & |
Assigns the state of other. | |
template<typename U = T> requires ( is_assignable_v<T&, U> and is_constructible_v<T, U> and not is_same_v<optional, decay_t<U>> and not is_scalar_v<T> and not is_same_v<T, decay_t<U>> ) | |
constexpr auto | operator= (U &&value) -> optional & |
Perfect-forwarded assignment. | |
template<typename F > requires (move_constructible<T> and same_as<remove_cvref_t<invoke_result_t<F>>, optional>) | |
constexpr auto | or_else (F &&f) &&-> optional |
template<typename F > requires (copy_constructible<T> and same_as<remove_cvref_t<invoke_result_t<F>>, optional>) | |
constexpr auto | or_else (F &&f) const &-> optional |
constexpr auto | reset () noexcept -> void |
If *this contains a value, destroy that value as if by value().~value_type(). Otherwise, there are no effects. *this does not contain a value after this call. | |
constexpr auto | swap (optional &other) noexcept(is_nothrow_move_constructible_v< value_type > and is_nothrow_swappable_v< value_type >) -> void |
Swaps the contents with those of other. | |
template<typename U > | |
constexpr auto | value_or (U &&defaultValue) &&-> value_type |
Returns the contained value if *this has a value, otherwise returns default_value. | |
template<typename U > | |
constexpr auto | value_or (U &&defaultValue) const &-> value_type |
Returns the contained value if *this has a value, otherwise returns default_value. | |
Related Symbols | |
(Note that these are not member symbols.) | |
template<typename T > | |
optional (T) -> optional< T > | |
template<typename T , typename... Args> | |
constexpr auto | make_optional (Args &&... args) -> etl::optional< T > |
Creates an optional object constructed in-place from args... | |
template<typename T > | |
constexpr auto | make_optional (T &&value) -> etl::optional< etl::decay_t< T > > |
Creates an optional object from value. | |
template<typename T > | |
constexpr auto | operator< (etl::nullopt_t, optional< T > const &opt) noexcept -> bool |
Compares opt with a nullopt. | |
template<typename T > | |
constexpr auto | operator< (optional< T > const &, etl::nullopt_t) noexcept -> bool |
Compares opt with a nullopt. | |
template<typename T , typename U > | |
constexpr auto | operator< (optional< T > const &lhs, optional< U > const &rhs) -> bool |
Compares two optional objects, lhs and rhs. | |
template<typename T , typename U > | |
constexpr auto | operator< (optional< T > const &opt, U const &value) -> bool |
Compares opt with a value. The values are compared (using the corresponding operator of T) only if opt contains a value. Otherwise, opt is considered less than value. If the corresponding two-way comparison expression between *opt and value is not well-formed, or if its result is not convertible to bool, the program is ill-formed. | |
template<typename T , typename U > | |
constexpr auto | operator< (T const &value, optional< U > const &opt) -> bool |
Compares opt with a value. The values are compared (using the corresponding operator of T) only if opt contains a value. Otherwise, opt is considered less than value. If the corresponding two-way comparison expression between *opt and value is not well-formed, or if its result is not convertible to bool, the program is ill-formed. | |
template<typename T , typename U > | |
constexpr auto | operator<= (optional< T > const &lhs, optional< U > const &rhs) -> bool |
Compares two optional objects, lhs and rhs. | |
template<typename T , typename U > | |
constexpr auto | operator<= (optional< T > const &opt, U const &value) -> bool |
Compares opt with a value. The values are compared (using the corresponding operator of T) only if opt contains a value. Otherwise, opt is considered less than value. If the corresponding two-way comparison expression between *opt and value is not well-formed, or if its result is not convertible to bool, the program is ill-formed. | |
template<typename T , typename U > | |
constexpr auto | operator<= (T const &value, optional< U > const &opt) -> bool |
Compares opt with a value. The values are compared (using the corresponding operator of T) only if opt contains a value. Otherwise, opt is considered less than value. If the corresponding two-way comparison expression between *opt and value is not well-formed, or if its result is not convertible to bool, the program is ill-formed. | |
template<typename T > | |
constexpr auto | operator== (etl::nullopt_t, optional< T > const &opt) noexcept -> bool |
Compares opt with a nullopt. | |
template<typename T , typename U > | |
constexpr auto | operator== (optional< T > const &lhs, optional< U > const &rhs) -> bool |
Compares two optional objects, lhs and rhs. | |
template<typename T > | |
constexpr auto | operator== (optional< T > const &opt, etl::nullopt_t) noexcept -> bool |
Compares opt with a nullopt. | |
template<typename T , typename U > | |
constexpr auto | operator== (optional< T > const &opt, U const &value) -> bool |
Compares opt with a value. The values are compared (using the corresponding operator of T) only if opt contains a value. Otherwise, opt is considered less than value. If the corresponding two-way comparison expression between *opt and value is not well-formed, or if its result is not convertible to bool, the program is ill-formed. | |
template<typename T , typename U > | |
constexpr auto | operator> (optional< T > const &lhs, optional< U > const &rhs) -> bool |
Compares two optional objects, lhs and rhs. | |
template<typename T , typename U > | |
constexpr auto | operator> (optional< T > const &opt, U const &value) -> bool |
Compares opt with a value. The values are compared (using the corresponding operator of T) only if opt contains a value. Otherwise, opt is considered less than value. If the corresponding two-way comparison expression between *opt and value is not well-formed, or if its result is not convertible to bool, the program is ill-formed. | |
template<typename T , typename U > | |
constexpr auto | operator> (T const &value, optional< U > const &opt) -> bool |
Compares opt with a value. The values are compared (using the corresponding operator of T) only if opt contains a value. Otherwise, opt is considered less than value. If the corresponding two-way comparison expression between *opt and value is not well-formed, or if its result is not convertible to bool, the program is ill-formed. | |
template<typename T , typename U > | |
constexpr auto | operator>= (optional< T > const &lhs, optional< U > const &rhs) -> bool |
Compares two optional objects, lhs and rhs. | |
template<typename T , typename U > | |
constexpr auto | operator>= (optional< T > const &opt, U const &value) -> bool |
Compares opt with a value. The values are compared (using the corresponding operator of T) only if opt contains a value. Otherwise, opt is considered less than value. If the corresponding two-way comparison expression between *opt and value is not well-formed, or if its result is not convertible to bool, the program is ill-formed. | |
template<typename T , typename U > | |
constexpr auto | operator>= (T const &value, optional< U > const &opt) -> bool |
Compares opt with a value. The values are compared (using the corresponding operator of T) only if opt contains a value. Otherwise, opt is considered less than value. If the corresponding two-way comparison expression between *opt and value is not well-formed, or if its result is not convertible to bool, the program is ill-formed. | |
The class template optional manages an optional contained value, i.e. a value that may or may not be present.
A common use case for optional is the return value of a function that may fail. As opposed to other approaches, such as etl::pair<T,bool>, optional handles expensive-to-construct objects well and is more readable, as the intent is expressed explicitly.
Any instance of optional at any given point in time either contains a value or does not contain a value.
If an optional contains a value, the value is guaranteed to be allocated as part of the optional object footprint, i.e. no dynamic memory allocation ever takes place. Thus, an optional object models an object, not a pointer, even though operator*() and operator->() are defined.
When an object of type optional is contextually converted to bool, the conversion returns true if the object contains a value and false if it does not contain a value.
The optional object contains a value in the following conditions:
The object does not contain a value in the following conditions:
There are no optional references; a program is ill-formed if it instantiates an optional with a reference type. Alternatively, an optional of a etl::reference_wrapper of type T may be used to hold a reference. In addition, a program is ill-formed if it instantiates an optional with the (possibly cv-qualified) tag types etl::nullopt_t or etl::in_place_t.
https://en.cppreference.com/w/cpp/utility/optional
T | The type of the value to manage initialization state for. The type must meet the requirements of Destructible (in particular, array types are not allowed). |
using value_type = T |
Constructs an object that does not contain a value.
Constructs an object that does not contain a value.
Copy constructor.
Move constructor.
|
inlineexplicitconstexpr |
Constructs an optional object that contains a value, initialized as if direct-initializing.
|
inlineconstexprnoexcept |
|
inlineconstexpr |
Constructs the contained value in-place. If *this already contains a value before the call, the contained value is destroyed by calling its destructor.
|
inlineconstexprnoexcept |
|
inline |
Converting move constructor.
If other doesn't contain a value, constructs an optional object that does not contain a value. Otherwise, constructs an optional object that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression etl::move(*other).
|
inline |
Constructs an optional object that contains a value, initialized as if direct-initializing.
|
inline |
Converting copy constructor.
If other doesn't contain a value, constructs an optional object that does not contain a value. Otherwise, constructs an optional object that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression *other.
Checks whether *this contains a value.
Checks whether *this contains a value.
Returns a reference to the contained value.
This operator only checks whether the optional contains a value in debug builds! You can do so manually by using has_value() or simply operator bool(). Alternatively, if checked access is needed, value() or value_or() may be used.
https://en.cppreference.com/w/cpp/utility/optional/operator*
Returns a reference to the contained value.
This operator only checks whether the optional contains a value in debug builds! You can do so manually by using has_value() or simply operator bool(). Alternatively, if checked access is needed, value() or value_or() may be used.
https://en.cppreference.com/w/cpp/utility/optional/operator*
Returns a reference to the contained value.
This operator only checks whether the optional contains a value in debug builds! You can do so manually by using has_value() or simply operator bool(). Alternatively, if checked access is needed, value() or value_or() may be used.
https://en.cppreference.com/w/cpp/utility/optional/operator*
Returns a reference to the contained value.
This operator only checks whether the optional contains a value in debug builds! You can do so manually by using has_value() or simply operator bool(). Alternatively, if checked access is needed, value() or value_or() may be used.
https://en.cppreference.com/w/cpp/utility/optional/operator*
Returns a pointer to the contained value. The pointer is null if the optional is empty.
Returns a pointer to the contained value. The pointer is null if the optional is empty.
|
inlineconstexprnoexcept |
If *this contains a value before the call, the contained value is destroyed by calling its destructor as if by value().T::~T(). *this does not contain a value after this call.
Assigns the state of other.
Assigns the state of other.
|
inlineconstexpr |
Assigns the state of other.
|
inlineconstexpr |
Assigns the state of other.
|
inlineconstexpr |
Perfect-forwarded assignment.
Depending on whether *this contains a value before the call, the contained value is either direct-initialized from etl::forward<U>(value) or assigned from etl::forward<U>(value).
https://en.cppreference.com/w/cpp/utility/optional/operator%3D
|
inlineconstexpr |
|
inlineconstexpr |
If *this contains a value, destroy that value as if by value().~value_type(). Otherwise, there are no effects. *this does not contain a value after this call.
Swaps the contents with those of other.
|
inlineconstexpr |
Returns the contained value if *this has a value, otherwise returns default_value.
|
inlineconstexpr |
Returns the contained value if *this has a value, otherwise returns default_value.