tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
optional.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_OPTIONAL_OPTIONAL_HPP
4#define TETL_OPTIONAL_OPTIONAL_HPP
5
6#include <etl/_config/all.hpp>
7
35#include <etl/_utility/move.hpp>
36#include <etl/_utility/swap.hpp>
39
40namespace etl {
41
88template <typename T>
89struct optional {
90 using value_type = T;
91
92 static_assert(!is_array_v<T>, "instantiation of optional with an array type is ill-formed");
93 static_assert(!is_same_v<remove_cvref_t<T>, nullopt_t>, "instantiation of optional with nullopt_t is ill-formed");
94 static_assert(!is_same_v<remove_cvref_t<T>, in_place_t>, "instantiation of optional with in_place_t is ill-formed");
95
97 constexpr optional() noexcept = default;
98
100 constexpr optional(nullopt_t /*null*/) noexcept { }
101
103 constexpr optional(optional const&) = default;
104
107
117 template <typename U>
118 // clang-format off
119 requires (
120 is_constructible_v<T, U const&>
121 and not is_same_v<remove_cv_t<U>, bool>
122 and not is_constructible_v<T, optional<U>&>
123 and not is_constructible_v<T, optional<U> const&>
124 and not is_constructible_v<T, optional<U> &&>
125 and not is_constructible_v<T, optional<U> const&&>
126 and not is_convertible_v<optional<U>&, T>
127 and not is_convertible_v<optional<U> const&, T>
128 and not is_convertible_v<optional<U>&&, T>
129 and not is_convertible_v<optional<U> const&&, T>
130
131 )
132 // clang-format on
133 explicit(not is_convertible_v<U const&, T>) constexpr optional(optional<U> const& other)
134 {
135 if (other.has_value()) {
136 emplace(*other);
137 }
138 }
139
148 template <typename U>
149 // clang-format off
150 requires (
152 and not is_same_v<remove_cv_t<U>, bool>
156 and not is_constructible_v<T, optional<U> const&&>
158 and not is_convertible_v<optional<U> const&, T>
159 and not is_convertible_v<optional<U>&&, T>
160 and not is_convertible_v<optional<U> const&&, T>
161 )
162 // clang-format on
163 explicit(not is_convertible_v<U&&, T>) constexpr optional(optional<U>&& other)
164 {
165 if (other.has_value()) {
166 emplace(*etl::move(other));
167 }
168 }
169
174 template <typename... Args>
175 requires is_constructible_v<T, Args...>
176 constexpr explicit optional(in_place_t /*tag*/, Args&&... args)
177 : _var(in_place_index<1>, etl::forward<Args>(args)...)
178 {
179 }
180
185 template <typename U = T>
186 // clang-format off
187 requires (
191 )
192 // clang-format on
193 explicit(not is_convertible_v<U&&, T>) constexpr optional(U&& value)
194 : _var(in_place_index<1>, etl::forward<U>(value))
195 {
196 }
197
201 constexpr auto operator=(etl::nullopt_t /*unused*/) noexcept -> optional&
202 {
203 reset();
204 return *this;
205 }
206
208 constexpr auto operator=(optional const& other) -> optional& = default;
209
211 constexpr auto operator=(optional&& other) noexcept -> optional& = default;
212
220 template <typename U = T>
221 // clang-format off
222 requires (
226 and not is_scalar_v<T>
228 )
229 // clang-format on
230 constexpr auto operator=(U&& value) -> optional&
231 {
232 emplace(etl::forward<U>(value));
233 return *this;
234 }
235
237 template <typename U = T>
238 // clang-format off
239 requires (
245 and not is_constructible_v<T, optional<U> const&&>
247 and not is_convertible_v<optional<U> const&, T>
248 and not is_convertible_v<optional<U>&&, T>
249 and not is_convertible_v<optional<U> const&&, T>
251 and not is_assignable_v<T&, optional<U> const&>
253 and not is_assignable_v<T&, optional<U> const&&>
254 )
255 // clang-format on
256 constexpr auto operator=(optional<U> const& other) -> optional&
257 {
258 if (other.has_value()) {
259 emplace(*other);
260 } else {
261 reset();
262 }
263
264 return *this;
265 }
266
268 template <typename U = T>
269 // clang-format off
270 requires (
276 and not is_constructible_v<T, optional<U> const&&>
278 and not is_convertible_v<optional<U> const&, T>
279 and not is_convertible_v<optional<U>&&, T>
280 and not is_convertible_v<optional<U> const&&, T>
282 and not is_assignable_v<T&, optional<U> const&>
284 and not is_assignable_v<T&, optional<U> const&&>
285 )
286 // clang-format on
287 constexpr auto operator=(optional<U>&& other) -> optional&
288 {
289 if (other.has_value()) {
290 emplace(*etl::move(other));
291 } else {
292 reset();
293 }
294
295 return *this;
296 }
297
299 [[nodiscard]] constexpr auto has_value() const noexcept -> bool { return _var.index() == 1; }
300
302 [[nodiscard]] constexpr explicit operator bool() const noexcept { return has_value(); }
303
307 constexpr auto reset() noexcept -> void { _var.template emplace<0>(nullopt); }
308
311 template <typename U>
312 [[nodiscard]] constexpr auto value_or(U&& defaultValue) const& -> value_type
313 {
314 return has_value() ? (**this) : static_cast<value_type>(etl::forward<U>(defaultValue));
315 }
316
319 template <typename U>
320 [[nodiscard]] constexpr auto value_or(U&& defaultValue) && -> value_type
321 {
322 return has_value() ? etl::move((**this)) : static_cast<value_type>(etl::forward<U>(defaultValue));
323 }
324
327 [[nodiscard]] constexpr auto operator->() const -> value_type const* { return etl::get_if<1>(&_var); }
328
331 [[nodiscard]] constexpr auto operator->() -> value_type* { return etl::get_if<1>(&_var); }
332
341 [[nodiscard]] constexpr auto operator*() const& -> T const&
342 {
344 return etl::unchecked_get<1>(_var);
345 }
346
355 [[nodiscard]] constexpr auto operator*() & -> T&
356 {
358 return etl::unchecked_get<1>(_var);
359 }
360
369 [[nodiscard]] constexpr auto operator*() const&& -> T const&&
370 {
372 return etl::move(etl::unchecked_get<1>(_var));
373 }
374
383 [[nodiscard]] constexpr auto operator*() && -> T&&
384 {
386 return etl::move(etl::unchecked_get<1>(_var));
387 }
388
390 constexpr auto swap(optional& other)
392 {
393 etl::swap(*this, other);
394 }
395
399 template <typename... Args>
400 constexpr auto emplace(Args&&... args) -> value_type&
401 {
402 return _var.template emplace<1>(etl::forward<Args>(args)...);
403 }
404
405 template <typename F>
406 constexpr auto and_then(F&& f) &
407 {
408 if (*this) {
409 return etl::invoke(etl::forward<F>(f), **this);
410 }
412 }
413
414 template <typename F>
415 constexpr auto and_then(F&& f) const&
416 {
417 if (*this) {
418 return etl::invoke(etl::forward<F>(f), **this);
419 }
421 }
422
423 template <typename F>
424 constexpr auto and_then(F&& f) &&
425 {
426 if (*this) {
427 return etl::invoke(etl::forward<F>(f), etl::move(**this));
428 }
430 }
431
432 template <typename F>
433 constexpr auto and_then(F&& f) const&&
434 {
435 if (*this) {
436 return etl::invoke(etl::forward<F>(f), etl::move(**this));
437 }
439 }
440
441 template <typename F>
443 constexpr auto or_else(F&& f) const& -> optional
444 {
445 return *this ? *this : etl::forward<F>(f)();
446 }
447
448 template <typename F>
450 constexpr auto or_else(F&& f) && -> optional
451 {
452 return *this ? etl::move(*this) : etl::forward<F>(f)();
453 }
454
455private:
456 variant<nullopt_t, T> _var{nullopt};
457};
458
459// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2988r3.pdf
460template <typename T>
461struct optional<T&> {
462 using value_type = T&;
463
464 constexpr optional() noexcept = default;
465
466 constexpr optional(nullopt_t /*tag*/) noexcept
467 : _ptr{nullptr}
468 {
469 }
470
471 template <typename U = T>
473 constexpr explicit(not is_convertible_v<U, T>) optional(U&& v)
474 : _ptr(etl::addressof(v))
475 {
476 static_assert(is_constructible_v<add_lvalue_reference_t<T>, U>, "Must be able to bind U to T&");
477 static_assert(is_lvalue_reference_v<U>, "U must be an lvalue");
478 }
479
480 template <typename U>
482 constexpr explicit(not is_convertible_v<U, T>) optional(optional<U> const& rhs)
483 : _ptr(etl::addressof(*rhs))
484 {
485 }
486
487 constexpr optional(optional const& other) = default;
488 constexpr optional(optional&& other) noexcept = default;
489 constexpr ~optional() = default;
490
491 constexpr auto operator=(optional const&) noexcept -> optional& = default;
492 constexpr auto operator=(optional&&) noexcept -> optional& = default;
493
494 constexpr auto operator=(nullopt_t /*tag*/) noexcept -> optional&
495 {
496 _ptr = nullptr;
497 return *this;
498 }
499
500 template <typename U = T>
502 constexpr auto operator=(U&& v) -> optional&
503 {
504 static_assert(is_constructible_v<add_lvalue_reference_t<T>, U>, "Must be able to bind U to T&");
505 static_assert(is_lvalue_reference_v<U>, "U must be an lvalue");
506 _ptr = etl::addressof(v);
507 return *this;
508 }
509
510 template <typename U>
511 constexpr auto operator=(optional<U> const& rhs) -> optional&
512 {
513 static_assert(is_constructible_v<add_lvalue_reference_t<T>, U>, "Must be able to bind U to T&");
514 _ptr = rhs._ptr;
515 return *this;
516 }
517
518 template <typename U = T>
520 constexpr auto emplace(U&& u) noexcept -> optional&
521 {
522 *this = etl::forward<U>(u);
523 return *this;
524 }
525
526 [[nodiscard]] constexpr auto operator->() const noexcept -> T* { return _ptr; }
527
528 [[nodiscard]] constexpr auto operator*() const noexcept -> T&
529 {
531 return *_ptr;
532 }
533
534 [[nodiscard]] constexpr explicit operator bool() const noexcept { return has_value(); }
535
536 [[nodiscard]] constexpr auto has_value() const noexcept -> bool { return _ptr != nullptr; }
537
538 constexpr void reset() noexcept { _ptr = nullptr; }
539
540 constexpr void swap(optional& rhs) noexcept { etl::swap(_ptr, rhs._ptr); }
541
542private:
543 T* _ptr{nullptr};
544};
545
549template <typename T>
550constexpr auto make_optional(T&& value) -> etl::optional<etl::decay_t<T>>
551{
553}
554
558template <typename T, typename... Args>
559constexpr auto make_optional(Args&&... args) -> etl::optional<T>
560{
561 return etl::optional<T>(etl::in_place, etl::forward<Args>(args)...);
562}
563
564// One deduction guide is provided for etl::optional to account for the
565// edge cases missed by the implicit deduction guides, in particular,
566// non-copyable arguments and array to pointer conversion.
569template <typename T>
571
575template <typename T, typename U>
576[[nodiscard]] constexpr auto operator==(optional<T> const& lhs, optional<U> const& rhs) -> bool
577{
578 if (static_cast<bool>(lhs) != static_cast<bool>(rhs)) {
579 return false;
580 }
581 if (not static_cast<bool>(lhs) and not static_cast<bool>(rhs)) {
582 return true;
583 }
584 return (*lhs) == (*rhs);
585}
586
590template <typename T, typename U>
591[[nodiscard]] constexpr auto operator<(optional<T> const& lhs, optional<U> const& rhs) -> bool
592{
593 if (not static_cast<bool>(rhs)) {
594 return false;
595 }
596 if (not static_cast<bool>(lhs)) {
597 return true;
598 }
599 return (*lhs) < (*rhs);
600}
601
605template <typename T, typename U>
606[[nodiscard]] constexpr auto operator>(optional<T> const& lhs, optional<U> const& rhs) -> bool
607{
608 if (not static_cast<bool>(lhs)) {
609 return false;
610 }
611 if (not static_cast<bool>(rhs)) {
612 return true;
613 }
614 return (*lhs) > (*rhs);
615}
616
620template <typename T, typename U>
621[[nodiscard]] constexpr auto operator<=(optional<T> const& lhs, optional<U> const& rhs) -> bool
622{
623 if (not static_cast<bool>(lhs)) {
624 return true;
625 }
626 if (not static_cast<bool>(rhs)) {
627 return false;
628 }
629 return (*lhs) <= (*rhs);
630}
631
635template <typename T, typename U>
636[[nodiscard]] constexpr auto operator>=(optional<T> const& lhs, optional<U> const& rhs) -> bool
637{
638 if (not static_cast<bool>(rhs)) {
639 return true;
640 }
641 if (not static_cast<bool>(lhs)) {
642 return false;
643 }
644 return (*lhs) >= (*rhs);
645}
646
653template <typename T>
654[[nodiscard]] constexpr auto operator==(optional<T> const& opt, etl::nullopt_t /*unused*/) noexcept -> bool
655{
656 return not opt;
657}
658
665template <typename T>
666[[nodiscard]] constexpr auto operator==(etl::nullopt_t /*unused*/, optional<T> const& opt) noexcept -> bool
667{
668 return not opt;
669}
670
677template <typename T>
678[[nodiscard]] constexpr auto operator<(optional<T> const& /*opt*/, etl::nullopt_t /*unused*/) noexcept -> bool
679{
680 return false;
681}
682
689template <typename T>
690[[nodiscard]] constexpr auto operator<(etl::nullopt_t /*unused*/, optional<T> const& opt) noexcept -> bool
691{
692 return static_cast<bool>(opt);
693}
694
705template <typename T, typename U>
706[[nodiscard]] constexpr auto operator==(optional<T> const& opt, U const& value) -> bool
707{
708 return static_cast<bool>(opt) ? *opt == value : false;
709}
710
721template <typename T, typename U>
722[[nodiscard]] constexpr auto operator<(optional<T> const& opt, U const& value) -> bool
723{
724 return static_cast<bool>(opt) ? *opt < value : true;
725}
726
737template <typename T, typename U>
738[[nodiscard]] constexpr auto operator<(T const& value, optional<U> const& opt) -> bool
739{
740 return static_cast<bool>(opt) ? value < *opt : false;
741}
742
753template <typename T, typename U>
754[[nodiscard]] constexpr auto operator>(optional<T> const& opt, U const& value) -> bool
755{
756 return static_cast<bool>(opt) ? *opt > value : false;
757}
758
769template <typename T, typename U>
770[[nodiscard]] constexpr auto operator>(T const& value, optional<U> const& opt) -> bool
771{
772 return static_cast<bool>(opt) ? value > *opt : true;
773}
774
785template <typename T, typename U>
786[[nodiscard]] constexpr auto operator<=(optional<T> const& opt, U const& value) -> bool
787{
788 return static_cast<bool>(opt) ? *opt <= value : true;
789}
790
801template <typename T, typename U>
802[[nodiscard]] constexpr auto operator<=(T const& value, optional<U> const& opt) -> bool
803{
804 return static_cast<bool>(opt) ? value <= *opt : false;
805}
806
817template <typename T, typename U>
818[[nodiscard]] constexpr auto operator>=(optional<T> const& opt, U const& value) -> bool
819{
820 return static_cast<bool>(opt) ? *opt >= value : false;
821}
822
833template <typename T, typename U>
834[[nodiscard]] constexpr auto operator>=(T const& value, optional<U> const& opt) -> bool
835{
836 return static_cast<bool>(opt) ? value >= *opt : true;
837}
838
857template <typename T>
858struct hash<etl::optional<T>> {
859 [[nodiscard]] constexpr auto operator()(etl::optional<T> const& opt) const -> etl::size_t
860 {
861 using type = etl::remove_const_t<T>;
863 return static_cast<bool>(opt) ? etl::hash<type>{}(*opt) : 0;
864 }
865};
866} // namespace etl
867
868#endif // TETL_OPTIONAL_OPTIONAL_HPP
#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 > &param) 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