The class template optional manages an optional contained value, i.e. a value that may or may not be present.
More...
|
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 && |
|
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.
|
|
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.
|
|
|
(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.
|
|
template<typename T>
struct etl::optional< T >
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 is initialized with/assigned from a value of type T or another optional that contains a value.
The object does not contain a value in the following conditions:
- The object is default-initialized.
- The object is initialized with/assigned from a value of type etl::nullopt_t or an optional object that does not contain a value.
- The member function reset() is called.
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
- Template Parameters
-
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). |
#undef NDEBUG
auto main() -> int
{
using etl::nullopt;
assert(opt0.has_value() ==
false);
assert(
static_cast<bool>(opt0) ==
false);
assert(opt1.has_value() ==
false);
assert(
static_cast<bool>(opt1) ==
false);
assert(
static_cast<bool>(opt2));
auto const opt3 = opt2;
assert(
static_cast<bool>(opt3));
assert(
static_cast<bool>(opt4));
opt5.reset();
assert(opt5.has_value() ==
false);
return 0;
}
#define assert(...)
Definition cassert.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
The class template optional manages an optional contained value, i.e. a value that may or may not be ...
Definition optional.hpp:89
constexpr optional() noexcept=default
Constructs an object that does not contain a value.
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
- Examples
- optional.cpp.