3#ifndef TETL_OPTIONAL_OPTIONAL_HPP
4#define TETL_OPTIONAL_OPTIONAL_HPP
92 static_assert(!
is_array_v<T>,
"instantiation of optional with an array type is ill-formed");
117 template <typename U>
135 if (other.has_value()) {
148 template <
typename U>
165 if (other.has_value()) {
174 template <
typename... Args>
177 : _var(in_place_index<1>,
etl::
forward<Args>(args)...)
185 template <
typename U = T>
194 : _var(in_place_index<1>,
etl::
forward<U>(value))
220 template <
typename U = T>
237 template <
typename U = T>
268 template <
typename U = T>
299 [[nodiscard]]
constexpr auto has_value() const noexcept ->
bool {
return _var.index() == 1; }
302 [[nodiscard]]
constexpr explicit operator bool() const noexcept {
return has_value(); }
311 template <
typename U>
319 template <
typename U>
341 [[nodiscard]]
constexpr auto operator*() const& -> T const&
369 [[nodiscard]]
constexpr auto operator*() const&& -> T const&&
399 template <
typename... Args>
405 template <
typename F>
414 template <
typename F>
423 template <
typename F>
432 template <
typename F>
441 template <
typename F>
448 template <
typename F>
471 template <
typename U = T>
480 template <
typename U>
500 template <
typename U = T>
510 template <
typename U>
518 template <
typename U = T>
526 [[nodiscard]]
constexpr auto operator->() const noexcept -> T* {
return _ptr; }
528 [[nodiscard]]
constexpr auto operator*() const noexcept -> T&
534 [[nodiscard]]
constexpr explicit operator bool() const noexcept {
return has_value(); }
536 [[nodiscard]]
constexpr auto has_value() const noexcept ->
bool {
return _ptr !=
nullptr; }
538 constexpr void reset() noexcept { _ptr =
nullptr; }
558template <
typename T,
typename... Args>
575template <
typename T,
typename U>
578 if (
static_cast<bool>(lhs) !=
static_cast<bool>(rhs)) {
581 if (not
static_cast<bool>(lhs) and not
static_cast<bool>(rhs)) {
584 return (*lhs) == (*rhs);
590template <
typename T,
typename U>
593 if (not
static_cast<bool>(rhs)) {
596 if (not
static_cast<bool>(lhs)) {
599 return (*lhs) < (*rhs);
605template <
typename T,
typename U>
608 if (not
static_cast<bool>(lhs)) {
611 if (not
static_cast<bool>(rhs)) {
614 return (*lhs) > (*rhs);
620template <
typename T,
typename U>
623 if (not
static_cast<bool>(lhs)) {
626 if (not
static_cast<bool>(rhs)) {
629 return (*lhs) <= (*rhs);
635template <
typename T,
typename U>
638 if (not
static_cast<bool>(rhs)) {
641 if (not
static_cast<bool>(lhs)) {
644 return (*lhs) >= (*rhs);
692 return static_cast<bool>(opt);
705template <
typename T,
typename U>
708 return static_cast<bool>(opt) ? *opt == value :
false;
721template <
typename T,
typename U>
724 return static_cast<bool>(opt) ? *opt < value :
true;
737template <
typename T,
typename U>
740 return static_cast<bool>(opt) ? value < *opt :
false;
753template <
typename T,
typename U>
756 return static_cast<bool>(opt) ? *opt > value :
false;
769template <
typename T,
typename U>
772 return static_cast<bool>(opt) ? value > *opt :
true;
785template <
typename T,
typename U>
788 return static_cast<bool>(opt) ? *opt <= value :
true;
801template <
typename T,
typename U>
804 return static_cast<bool>(opt) ? value <= *opt :
false;
817template <
typename T,
typename U>
820 return static_cast<bool>(opt) ? *opt >= value :
false;
833template <
typename T,
typename U>
836 return static_cast<bool>(opt) ? value >= *opt :
true;
#define TETL_PRECONDITION(...)
Definition check.hpp:16
The concept copy_constructible is satisfied if T is an lvalue reference type, or if it is a move_cons...
Definition copy_constructible.hpp:20
The concept move_constructible is satisfied if T is a reference type, or if it is an object type wher...
Definition move_constructible.hpp:20
The concept same_as<T, U> is satisfied if and only if T and U denote the same type....
Definition same_as.hpp:19
constexpr auto move(InputIt first, InputIt last, OutputIt destination) -> OutputIt
Moves the elements in the range [first, last), to another range beginning at destination,...
Definition move.hpp:26
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 op...
Definition optional.hpp:818
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 op...
Definition optional.hpp:706
constexpr auto operator>(optional< T > const &lhs, optional< U > const &rhs) -> bool
Compares two optional objects, lhs and rhs.
Definition optional.hpp:606
optional(T) -> optional< T >
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 op...
Definition optional.hpp:754
constexpr auto operator==(etl::nullopt_t, optional< T > const &opt) noexcept -> bool
Compares opt with a nullopt.
Definition optional.hpp:666
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 op...
Definition optional.hpp:802
constexpr auto make_optional(Args &&... args) -> etl::optional< T >
Creates an optional object constructed in-place from args...
Definition optional.hpp:559
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 op...
Definition optional.hpp:786
constexpr auto operator>=(optional< T > const &lhs, optional< U > const &rhs) -> bool
Compares two optional objects, lhs and rhs.
Definition optional.hpp:636
constexpr auto operator<=(optional< T > const &lhs, optional< U > const &rhs) -> bool
Compares two optional objects, lhs and rhs.
Definition optional.hpp:621
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 op...
Definition optional.hpp:834
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 op...
Definition optional.hpp:738
constexpr auto operator<(optional< T > const &, etl::nullopt_t) noexcept -> bool
Compares opt with a nullopt.
Definition optional.hpp:678
constexpr auto operator==(optional< T > const &lhs, optional< U > const &rhs) -> bool
Compares two optional objects, lhs and rhs.
Definition optional.hpp:576
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 op...
Definition optional.hpp:722
constexpr auto operator<(optional< T > const &lhs, optional< U > const &rhs) -> bool
Compares two optional objects, lhs and rhs.
Definition optional.hpp:591
constexpr auto make_optional(T &&value) -> etl::optional< etl::decay_t< T > >
Creates an optional object from value.
Definition optional.hpp:550
constexpr auto operator<(etl::nullopt_t, optional< T > const &opt) noexcept -> bool
Compares opt with a nullopt.
Definition optional.hpp:690
constexpr auto operator==(optional< T > const &opt, etl::nullopt_t) noexcept -> bool
Compares opt with a nullopt.
Definition optional.hpp:654
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 op...
Definition optional.hpp:770
Definition adjacent_find.hpp:8
constexpr bool is_scalar_v
Definition is_scalar.hpp:40
constexpr bool is_lvalue_reference_v
Definition is_lvalue_reference.hpp:20
constexpr bool is_constructible_v
Definition is_constructible.hpp:24
remove_cv_t< remove_reference_t< T > > remove_cvref_t
Definition remove_cvref.hpp:23
constexpr auto addressof(T &arg) noexcept -> T *
Obtains the actual address of the object or function arg, even in presence of overloaded operator&.
Definition addressof.hpp:15
constexpr bool is_specialized_v
Definition is_specialized.hpp:20
constexpr bool is_nothrow_move_constructible_v
Definition is_nothrow_move_constructible.hpp:20
constexpr bool conjunction_v
Definition conjunction.hpp:17
remove_const_t< remove_volatile_t< T > > remove_cv_t
Definition remove_cv.hpp:22
typename remove_const< T >::type remove_const_t
Definition remove_const.hpp:23
constexpr bool is_assignable_v
Definition is_assignable.hpp:20
constexpr bool is_array_v
Definition is_array.hpp:26
constexpr auto unchecked_get(variant< Ts... > &v) -> auto &
constexpr bool is_same_v
Definition is_same.hpp:11
constexpr auto invoke(F &&f, Args &&... args) -> invoke_result_t< F, Args... >
Definition invoke.hpp:45
auto swap(inplace_function< R(Args...), Capacity, Alignment > &lhs, inplace_function< R(Args...), Capacity, Alignment > &rhs) noexcept -> void
Overloads the etl::swap algorithm for etl::inplace_function. Exchanges the state of lhs with that of ...
Definition inplace_function.hpp:249
constexpr auto get_if(variant< Types... > *pv) noexcept -> add_pointer_t< T >
constexpr bool is_convertible_v
Definition is_convertible.hpp:46
constexpr auto forward(remove_reference_t< T > ¶m) noexcept -> T &&
Forwards lvalues as either lvalues or as rvalues, depending on T. When t is a forwarding reference (a...
Definition forward.hpp:18
TETL_BUILTIN_SIZET size_t
etl::size_t is the unsigned integer type of the result of the sizeof operator.
Definition size_t.hpp:14
constexpr bool is_nothrow_swappable_v
Definition is_nothrow_swappable.hpp:19
constexpr auto operator()(etl::optional< T > const &opt) const -> etl::size_t
Definition optional.hpp:859
hash
Definition hash.hpp:14
Disambiguation tags that can be passed to the constructors of optional, variant, and any to indicate ...
Definition in_place.hpp:20
If T and U name the same type (taking into account const/volatile qualifications),...
Definition is_same.hpp:20
etl::nullopt_t is an empty class type used to indicate optional type with uninitialized state....
Definition nullopt.hpp:13
constexpr auto emplace(U &&u) noexcept -> optional &
Definition optional.hpp:520
constexpr optional(optional &&other) noexcept=default
constexpr void swap(optional &rhs) noexcept
Definition optional.hpp:540
T & value_type
Definition optional.hpp:462
constexpr optional() noexcept=default
constexpr auto operator=(U &&v) -> optional &
Definition optional.hpp:502
constexpr auto operator=(optional &&) noexcept -> optional &=default
constexpr optional(optional const &other)=default
constexpr auto operator=(optional const &) noexcept -> optional &=default
constexpr ~optional()=default
constexpr void reset() noexcept
Definition optional.hpp:538
constexpr auto operator=(optional< U > const &rhs) -> optional &
Definition optional.hpp:511
constexpr auto operator->() const noexcept -> T *
Definition optional.hpp:526
constexpr auto has_value() const noexcept -> bool
Definition optional.hpp:536
constexpr auto operator*() const noexcept -> T &
Definition optional.hpp:528
The class template optional manages an optional contained value, i.e. a value that may or may not be ...
Definition optional.hpp:89
explicit(not is_convertible_v< U const &, T >) const expr optional(optional< U > const &other)
Converting copy constructor.
Definition optional.hpp:133
constexpr auto value_or(U &&defaultValue) &&-> value_type
Returns the contained value if *this has a value, otherwise returns default_value.
Definition optional.hpp:320
constexpr auto operator=(optional const &other) -> optional &=default
Assigns the state of other.
constexpr auto and_then(F &&f) &&
Definition optional.hpp:424
constexpr auto or_else(F &&f) const &-> optional
Definition optional.hpp:443
constexpr auto or_else(F &&f) &&-> optional
Definition optional.hpp:450
constexpr optional(in_place_t, Args &&... args)
Constructs an optional object that contains a value, initialized as if direct-initializing.
Definition optional.hpp:176
constexpr auto reset() noexcept -> void
If *this contains a value, destroy that value as if by value().~value_type(). Otherwise,...
Definition optional.hpp:307
constexpr auto operator=(optional &&other) noexcept -> optional &=default
Assigns the state of other.
constexpr auto operator*() const &-> T const &
Returns a reference to the contained value.
Definition optional.hpp:341
constexpr auto and_then(F &&f) &
Definition optional.hpp:406
constexpr optional() noexcept=default
Constructs an object that does not contain a value.
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.
Definition optional.hpp:390
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...
Definition optional.hpp:201
constexpr optional(optional &&) noexcept(is_nothrow_move_constructible_v< value_type >)=default
Move constructor.
constexpr auto value_or(U &&defaultValue) const &-> value_type
Returns the contained value if *this has a value, otherwise returns default_value.
Definition optional.hpp:312
constexpr optional(optional const &)=default
Copy constructor.
constexpr auto operator*() &-> T &
Returns a reference to the contained value.
Definition optional.hpp:355
constexpr auto operator->() -> value_type *
Returns a pointer to the contained value. The pointer is null if the optional is empty.
Definition optional.hpp:331
constexpr auto has_value() const noexcept -> bool
Checks whether *this contains a value.
Definition optional.hpp:299
constexpr auto and_then(F &&f) const &&
Definition optional.hpp:433
constexpr auto and_then(F &&f) const &
Definition optional.hpp:415
constexpr auto operator*() const &&-> T const &&
Returns a reference to the contained value.
Definition optional.hpp:369
constexpr auto operator->() const -> value_type const *
Returns a pointer to the contained value. The pointer is null if the optional is empty.
Definition optional.hpp:327
constexpr auto operator*() &&-> T &&
Returns a reference to the contained value.
Definition optional.hpp:383
constexpr auto emplace(Args &&... args) -> value_type &
Constructs the contained value in-place. If *this already contains a value before the call,...
Definition optional.hpp:400
T value_type
Definition optional.hpp:90
Definition variant.hpp:98