diff --git a/docs/index.md b/docs/index.md index a42fec2..a27e863 100644 --- a/docs/index.md +++ b/docs/index.md @@ -336,43 +336,31 @@ public: void swap(expected& rhs) noexcept(std::is_nothrow_move_constructible<T>::value&&noexcept(swap(std::declval<T&>(), std::declval<T&>()))&&std::is_nothrow_move_constructible<E>::value&&noexcept(swap(std::declval<E&>(), std::declval<E&>()))); - constexpr const T* operator->() const; + constexpr const T* operator->() const; + T* operator->(); - T* operator->(); + constexpr const T& operator*() const &; + T& operator*() &; + constexpr const T&& operator*() const &&; + T&& operator*() &&; - constexpr const T& operator*() const &; + constexpr bool has_value() const noexcept; + constexpr operator bool() const noexcept; - T& operator*() &; + constexpr const T& value() const &; + T& value() &; + constexpr const T&& value() const &&; + T&& value() &&; - constexpr const T&& operator*() const &&; - - T&& operator*() &&; - - constexpr operator bool() const noexcept; - - constexpr bool has_value() const noexcept; - - constexpr const T& value() const &; - - T& value() &; - - constexpr const T&& value() const &&; - - T&& value() &&; - - constexpr const E& error() const &; - - E& error() &; - - constexpr const E&& error() const &&; - - E&& error() &&; + constexpr const E& error() const &; + E& error() &; + constexpr const E&& error() const &&; + E&& error() &&; template <class U> - constexpr T value_or(U&& v) const &; - + constexpr T value_or(U&& v) const &; template <class U> - T value_or(U&& v) &&; + T value_or(U&& v) &&; }; An `expected` object is an object that contains the storage for another object and manages the lifetime of this contained object `T`. Alternatively it could contain the storage for another unexpected object `E`. The contained object may not be initialized after the expected object has been initialized, and may not be destroyed before the expected object has been destroyed. The initialization state of the contained object is tracked by the expected object. @@ -438,6 +426,74 @@ Calls `f` if the expectd is in the unexpected state (2) EXPLICIT constexpr expected(unexpected<G> &&e); +### Operator `tl::expected::operator->` + +
(1)  constexpr const T* operator->() const;
+
+(2)  T* operator->();
+ +*Returns*: a pointer to the stored value + +*Requires*: a value is stored + +### Operator `tl::expected::operator*` + +
(1)  constexpr const T& operator*() const &;
+
+(2)  T& operator*() &;
+
+(3)  constexpr const T&& operator*() const &&;
+
+(4)  T&& operator*() &&;
+ +*Returns*: the stored value + +*Requires*: a value is stored + +### Function `tl::expected::has_value` + +
(1)  constexpr bool has_value() const noexcept;
+
+(2)  constexpr operator bool() const noexcept;
+ +*Returns*: whether or not the optional has a value + +### Function `tl::expected::value` + +
(1)  constexpr const T& value() const &;
+
+(2)  T& value() &;
+
+(3)  constexpr const T&& value() const &&;
+
+(4)  T&& value() &&;
+ +*Returns*: the contained value if there is one, otherwise throws \[bad\_expected\_access\] + +### Function `tl::expected::error` + +
(1)  constexpr const E& error() const &;
+
+(2)  E& error() &;
+
+(3)  constexpr const E&& error() const &&;
+
+(4)  E&& error() &&;
+ +*Returns*: the unexpected value + +*Requires*: there is an unexpected value + +### Function template `tl::expected::value_or` + +
(1)  template <class U>
+     constexpr T value_or(U&& v) const &;
+
+(2)  template <class U>
+     T value_or(U&& v) &&;
+ +*Returns*: the stored value if there is one, otherwise returns `u` + ----- ----- diff --git a/expected.hpp b/expected.hpp index 075030d..c9dda07 100644 --- a/expected.hpp +++ b/expected.hpp @@ -1541,44 +1541,77 @@ public: } } + /// \returns a pointer to the stored value + /// \requires a value is stored + /// \group pointer constexpr const T *operator->() const { return valptr(); } + /// \group pointer TL_EXPECTED_11_CONSTEXPR T *operator->() { return valptr(); } + + /// \returns the stored value + /// \requires a value is stored + /// \group deref constexpr const T &operator*() const & { return val(); } + /// \group deref TL_EXPECTED_11_CONSTEXPR T &operator*() & { return val(); } + /// \group deref constexpr const T &&operator*() const && { return std::move(val()); } + /// \group deref TL_EXPECTED_11_CONSTEXPR T &&operator*() && { return std::move(val()); } - constexpr explicit operator bool() const noexcept { return this->m_has_val; } + + /// \returns whether or not the optional has a value + /// \group has_value constexpr bool has_value() const noexcept { return this->m_has_val; } + /// \group has_value + constexpr explicit operator bool() const noexcept { return this->m_has_val; } + + + /// \returns the contained value if there is one, otherwise throws [bad_expected_access] + /// \group value constexpr const T &value() const & { if (!has_value()) throw bad_expected_access(err().value()); return val(); } + /// \group value TL_EXPECTED_11_CONSTEXPR T &value() & { if (!has_value()) throw bad_expected_access(err().value()); return val(); } + /// \group value constexpr const T &&value() const && { if (!has_value()) throw bad_expected_access(err().value()); return std::move(val()); } + /// \group value TL_EXPECTED_11_CONSTEXPR T &&value() && { if (!has_value()) throw bad_expected_access(err().value()); return std::move(val()); } + + /// \returns the unexpected value + /// \requires there is an unexpected value + /// \group error constexpr const E &error() const & { return err().value(); } + /// \group error TL_EXPECTED_11_CONSTEXPR E &error() & { return err().value(); } + /// \group error constexpr const E &&error() const && { return std::move(err().value()); } + /// \group error TL_EXPECTED_11_CONSTEXPR E &&error() && { return std::move(err().value()); } + + /// \returns the stored value if there is one, otherwise returns `u` + /// \group value_or template constexpr T value_or(U &&v) const & { static_assert(std::is_copy_constructible::value && std::is_convertible::value, "T must be copy-constructible and convertible to from U&&"); return bool(*this) ? **this : static_cast(std::forward(v)); } + /// \group value_or template TL_EXPECTED_11_CONSTEXPR T value_or(U &&v) && { static_assert(std::is_move_constructible::value && std::is_convertible::value,