22 General utilities library [utilities]

22.8 Expected objects [expected]

22.8.7 Partial specialization of expected for void types [expected.void]

22.8.7.1 General [expected.void.general]

template<class T, class E> requires is_void_v<T> class expected<T, E> { public: using value_type = T; using error_type = E; using unexpected_type = unexpected<E>; template<class U> using rebind = expected<U, error_type>; // [expected.void.cons], constructors constexpr expected() noexcept; constexpr expected(const expected&); constexpr expected(expected&&) noexcept(see below); template<class U, class G> constexpr explicit(see below) expected(const expected<U, G>&); template<class U, class G> constexpr explicit(see below) expected(expected<U, G>&&); template<class G> constexpr expected(const unexpected<G>&); template<class G> constexpr expected(unexpected<G>&&); constexpr explicit expected(in_place_t) noexcept; template<class... Args> constexpr explicit expected(unexpect_t, Args&&...); template<class U, class... Args> constexpr explicit expected(unexpect_t, initializer_list<U>, Args&&...); // [expected.void.dtor], destructor constexpr ~expected(); // [expected.void.assign], assignment constexpr expected& operator=(const expected&); constexpr expected& operator=(expected&&) noexcept(see below); template<class G> constexpr expected& operator=(const unexpected<G>&); template<class G> constexpr expected& operator=(unexpected<G>&&); constexpr void emplace() noexcept; // [expected.void.swap], swap constexpr void swap(expected&) noexcept(see below); friend constexpr void swap(expected& x, expected& y) noexcept(noexcept(x.swap(y))); // [expected.void.obs], observers constexpr explicit operator bool() const noexcept; constexpr bool has_value() const noexcept; constexpr void operator*() const noexcept; constexpr void value() const &; constexpr void value() &&; constexpr const E& error() const & noexcept; constexpr E& error() & noexcept; constexpr const E&& error() const && noexcept; constexpr E&& error() && noexcept; // [expected.void.eq], equality operators template<class T2, class E2> requires is_void_v<T2> friend constexpr bool operator==(const expected& x, const expected<T2, E2>& y); template<class E2> friend constexpr bool operator==(const expected&, const unexpected<E2>&); private: bool has_val; // exposition only union { E unex; // exposition only }; };
Any object of type expected<T, E> either represents a value of type T, or contains a value of type E within its own storage.
Implementations are not permitted to use additional storage, such as dynamic memory, to allocate the object of type E.
Member has_­val indicates whether the expected<T, E> object represents a value of type T.
A program that instantiates the definition of the template expected<T, E> with a type for the E parameter that is not a valid template argument for unexpected is ill-formed.
E shall meet the requirements of Cpp17Destructible (Table 36).

22.8.7.2 Constructors [expected.void.cons]

constexpr expected() noexcept;
Postconditions: has_­value() is true.
constexpr expected(const expected& rhs);
Effects: If rhs.has_­value() is false, direct-non-list-initializes unex with rhs.error().
Postconditions: rhs.has_­value() == this->has_­value().
Throws: Any exception thrown by the initialization of unex.
Remarks: This constructor is defined as deleted unless is_­copy_­constructible_­v<E> is true.
This constructor is trivial if is_­trivially_­copy_­constructible_­v<E> is true.
constexpr expected(expected&& rhs) noexcept(is_nothrow_move_constructible_v<E>);
Constraints: is_­move_­constructible_­v<E> is true.
Effects: If rhs.has_­value() is false, direct-non-list-initializes unex with std​::​move(rhs.error()).
Postconditions: rhs.has_­value() is unchanged; rhs.has_­value() == this->has_­value() is true.
Throws: Any exception thrown by the initialization of unex.
Remarks: This constructor is trivial if is_­trivially_­move_­constructible_­v<E> is true.
template<class U, class G> constexpr explicit(!is_convertible_v<const G&, E>) expected(const expected<U, G>& rhs); template<class U, class G> constexpr explicit(!is_convertible_v<G, E>) expected(expected<U, G>&& rhs);
Let GF be const G& for the first overload and G for the second overload.
Constraints:
  • is_­void_­v<U> is true; and
  • is_­constructible_­v<E, GF> is true; and
  • is_­constructible_­v<unexpected<E>, expected<U, G>&> is false; and
  • is_­constructible_­v<unexpected<E>, expected<U, G>> is false; and
  • is_­constructible_­v<unexpected<E>, const expected<U, G>&> is false; and
  • is_­constructible_­v<unexpected<E>, const expected<U, G>> is false.
Effects: If rhs.has_­value() is false, direct-non-list-initializes unex with std​::​forward<GF>(rhs.error()).
Postconditions: rhs.has_­value() is unchanged; rhs.has_­value() == this->has_­value() is true.
Throws: Any exception thrown by the initialization of unex.
template<class G> constexpr explicit(!is_convertible_v<const G&, E>) expected(const unexpected<G>& e); template<class G> constexpr explicit(!is_convertible_v<G, E>) expected(unexpected<G>&& e);
Let GF be const G& for the first overload and G for the second overload.
Constraints: is_­constructible_­v<E, GF> is true.
Effects: Direct-non-list-initializes unex with std​::​forward<GF>(e.error()).
Postconditions: has_­value() is false.
Throws: Any exception thrown by the initialization of unex.
constexpr explicit expected(in_place_t) noexcept;
Postconditions: has_­value() is true.
template<class... Args> constexpr explicit expected(unexpect_t, Args&&... args);
Constraints: is_­constructible_­v<E, Args...> is true.
Effects: Direct-non-list-initializes unex with std​::​forward<Args>(args)....
Postconditions: has_­value() is false.
Throws: Any exception thrown by the initialization of unex.
template<class U, class... Args> constexpr explicit expected(unexpect_t, initializer_list<U> il, Args&&... args);
Constraints: is_­constructible_­v<E, initializer_­list<U>&, Args...> is true.
Effects: Direct-non-list-initializes unex with il, std​::​forward<Args>(args)....
Postconditions: has_­value() is false.
Throws: Any exception thrown by the initialization of unex.

22.8.7.3 Destructor [expected.void.dtor]

constexpr ~expected();
Effects: If has_­value() is false, destroys unex.
Remarks: If is_­trivially_­destructible_­v<E> is true, then this destructor is a trivial destructor.

22.8.7.4 Assignment [expected.void.assign]

constexpr expected& operator=(const expected& rhs);
Effects:
  • If this->has_­value() && rhs.has_­value() is true, no effects.
  • Otherwise, if this->has_­value() is true, equivalent to: construct_­at(addressof(unex), rhs.unex); has_­val = false;
  • Otherwise, if rhs.has_­value() is true, destroys unex and sets has_­val to true.
  • Otherwise, equivalent to unex = rhs.error().
Returns: *this.
Remarks: This operator is defined as deleted unless is_­copy_­assignable_­v<E> is true and is_­copy_­constructible_­v<E> is true.
constexpr expected& operator=(expected&& rhs) noexcept(see below);
Effects:
  • If this->has_­value() && rhs.has_­value() is true, no effects.
  • Otherwise, if this->has_­value() is true, equivalent to: construct_at(addressof(unex), std::move(rhs.unex)); has_val = false;
  • Otherwise, if rhs.has_­value() is true, destroys unex and sets has_­val to true.
  • Otherwise, equivalent to unex = std​::​move(rhs.error()).
Returns: *this.
Remarks: The exception specification is equivalent to is_­nothrow_­move_­constructible_­v<E> && is_­nothrow_­move_­assignable_­v<E>.
This operator is defined as deleted unless is_­move_­constructible_­v<E> is true and is_­move_­assignable_­v<E> is true.
template<class G> constexpr expected& operator=(const unexpected<G>& e); template<class G> constexpr expected& operator=(unexpected<G>&& e);
Let GF be const G& for the first overload and G for the second overload.
Constraints: is_­constructible_­v<E, GF> is true and is_­assignable_­v<E&, GF> is true.
Effects:
  • If has_­value() is true, equivalent to: construct_at(addressof(unex), std::forward<GF>(e.error())); has_val = false;
  • Otherwise, equivalent to: unex = std​::​forward<GF>(e.error());
Returns: *this.
constexpr void emplace() noexcept;
Effects: If has_­value() is false, destroys unex and sets has_­val to true.

22.8.7.5 Swap [expected.void.swap]

constexpr void swap(expected& rhs) noexcept(see below);
Constraints: is_­swappable_­v<E> is true and is_­move_­constructible_­v<E> is true.
Effects: See Table 65.
Table 65: swap(expected&) effects [tab:expected.void.swap]
this->has_­value()
!this->has_­value()
rhs.has_­value()
no effects
calls rhs.swap(*this)
!rhs.has_­value()
see below
equivalent to: using std​::​swap; swap(unex, rhs.unex);
For the case where rhs.value() is false and this->has_­value() is true, equivalent to: construct_at(addressof(unex), std::move(rhs.unex)); destroy_at(addressof(rhs.unex)); has_val = false; rhs.has_val = true;
Throws: Any exception thrown by the expressions in the Effects.
Remarks: The exception specification is equivalent to is_­nothrow_­move_­constructible_­v<E> && is_­nothrow_­swappable_­v<E>.
friend constexpr void swap(expected& x, expected& y) noexcept(noexcept(x.swap(y)));
Effects: Equivalent to x.swap(y).

22.8.7.6 Observers [expected.void.obs]

constexpr explicit operator bool() const noexcept; constexpr bool has_value() const noexcept;
Returns: has_­val.
constexpr void operator*() const noexcept;
Preconditions: has_­value() is true.
constexpr void value() const &;
Throws: bad_­expected_­access(error()) if has_­value() is false.
constexpr void value() &&;
Throws: bad_­expected_­access(std​::​move(error())) if has_­value() is false.
constexpr const E& error() const & noexcept; constexpr E& error() & noexcept;
Preconditions: has_­value() is false.
Returns: unex.
constexpr E&& error() && noexcept; constexpr const E&& error() const && noexcept;
Preconditions: has_­value() is false.
Returns: std​::​move(unex).

22.8.7.7 Equality operators [expected.void.eq]

template<class T2, class E2> requires is_void_v<T2> friend constexpr bool operator==(const expected& x, const expected<T2, E2>& y);
Mandates: The expression x.error() == y.error() is well-formed and its result is convertible to bool.
Returns: If x.has_­value() does not equal y.has_­value(), false; otherwise x.has_­value() || static_­cast<bool>(x.error() == y.error()).
template<class E2> friend constexpr bool operator==(const expected& x, const unexpected<E2>& e);
Mandates: The expression x.error() == e.error() is well-formed and its result is convertible to bool.
Returns: !x.has_­value() && static_­cast<bool>(x.error() == e.error()).