22 General utilities library [utilities]

22.8 Expected objects [expected]

22.8.6 Class template expected [expected.expected]

22.8.6.2 Constructors [expected.object.ctor]

constexpr expected();
Constraints: is_­default_­constructible_­v<T> is true.
Effects: Value-initializes val.
Postconditions: has_­value() is true.
Throws: Any exception thrown by the initialization of val.
constexpr expected(const expected& rhs);
Effects: If rhs.has_­value() is true, direct-non-list-initializes val with *rhs.
Otherwise, direct-non-list-initializes unex with rhs.error().
Postconditions: rhs.has_­value() == this->has_­value().
Throws: Any exception thrown by the initialization of val or unex.
Remarks: This constructor is defined as deleted unless
  • is_­copy_­constructible_­v<T> is true and
  • is_­copy_­constructible_­v<E> is true.
This constructor is trivial if
  • is_­trivially_­copy_­constructible_­v<T> is true and
  • is_­trivially_­copy_­constructible_­v<E> is true.
constexpr expected(expected&& rhs) noexcept(see below);
Constraints:
  • is_­move_­constructible_­v<T> is true and
  • is_­move_­constructible_­v<E> is true.
Effects: If rhs.has_­value() is true, direct-non-list-initializes val with std​::​move(*rhs).
Otherwise, 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 val or unex.
Remarks: The exception specification is equivalent to is_­nothrow_­move_­constructible_­v<T> && is_­nothrow_­move_­constructible_­v<E>.
This constructor is trivial if
  • is_­trivially_­move_­constructible_­v<T> is true and
  • is_­trivially_­move_­constructible_­v<E> is true.
template<class U, class G> constexpr explicit(see below) expected(const expected<U, G>& rhs); template<class U, class G> constexpr explicit(see below) expected(expected<U, G>&& rhs);
Let:
  • UF be const U& for the first overload and U for the second overload.
  • GF be const G& for the first overload and G for the second overload.
Constraints:
  • is_­constructible_­v<T, UF> is true; and
  • is_­constructible_­v<E, GF> is true; and
  • is_­constructible_­v<T, expected<U, G>&> is false; and
  • is_­constructible_­v<T, expected<U, G>> is false; and
  • is_­constructible_­v<T, const expected<U, G>&> is false; and
  • is_­constructible_­v<T, const expected<U, G>> is false; and
  • is_­convertible_­v<expected<U, G>&, T> is false; and
  • is_­convertible_­v<expected<U, G>&&, T> is false; and
  • is_­convertible_­v<const expected<U, G>&, T> is false; and
  • is_­convertible_­v<const expected<U, G>&&, T> is false; 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(), direct-non-list-initializes val with std​::​forward<UF>(*rhs).
Otherwise, 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 val or unex.
Remarks: The expression inside explicit is equivalent to !is_­convertible_­v<UF, T> || !is_­convertible_­v<GF, E>.
template<class U = T> constexpr explicit(!is_convertible_v<U, T>) expected(U&& v);
Constraints:
  • is_­same_­v<remove_­cvref_­t<U>, in_­place_­t> is false; and
  • is_­same_­v<expected, remove_­cvref_­t<U>> is false; and
  • remove_­cvref_­t<U> is not a specialization of unexpected; and
  • is_­constructible_­v<T, U> is true.
Effects: Direct-non-list-initializes val with std​::​forward<U>(v).
Postconditions: has_­value() is true.
Throws: Any exception thrown by the initialization of val.
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.value()).
Postconditions: has_­value() is false.
Throws: Any exception thrown by the initialization of unex.
template<class... Args> constexpr explicit expected(in_place_t, Args&&... args);
Constraints: is_­constructible_­v<T, Args...> is true.
Effects: Direct-non-list-initializes val with std​::​forward<Args>(args)....
Postconditions: has_­value() is true.
Throws: Any exception thrown by the initialization of val.
template<class U, class... Args> constexpr explicit expected(in_place_t, initializer_list<U> il, Args&&... args);
Constraints: is_­constructible_­v<T, initializer_­list<U>&, Args...> is true.
Effects: Direct-non-list-initializes val with il, std​::​forward<Args>(args)....
Postconditions: has_­value() is true.
Throws: Any exception thrown by the initialization of val.
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.