22 General utilities library [utilities]

22.5 Optional objects [optional]

22.5.4 Partial specialization of optional for reference types [optional.optional.ref]

22.5.4.2 Constructors [optional.ref.ctor]

template<class Arg> constexpr explicit optional(in_place_t, Arg&& arg);
Constraints:
  • is_constructible_v<T&, Arg> is true, and
  • reference_constructs_from_temporary_v<T&, Arg> is false.
Effects: Equivalent to: convert-ref-init-val(std​::​forward<Arg>(arg)).
Postconditions: *this contains a value.
template<class U> constexpr explicit(!is_convertible_v<U, T&>) optional(U&& u) noexcept(is_nothrow_constructible_v<T&, U>);
Constraints:
  • is_same_v<remove_cvref_t<U>, optional> is false,
  • is_same_v<remove_cvref_t<U>, in_place_t> is false, and
  • is_constructible_v<T&, U> is true.
Effects: Equivalent to: convert-ref-init-val(std​::​forward<U>(u)).
Postconditions: *this contains a value.
Remarks: This constructor is defined as deleted if reference_constructs_from_temporary_v<T&, U> is true.
template<class U> constexpr explicit(!is_convertible_v<U&, T&>) optional(optional<U>& rhs) noexcept(is_nothrow_constructible_v<T&, U&>);
Constraints:
  • is_same_v<remove_cv_t<T>, optional<U>> is false,
  • is_same_v<T&, U> is false, and
  • is_constructible_v<T&, U&> is true.
Effects: Equivalent to: if (rhs.has_value()) convert-ref-init-val(*rhs);
Remarks: This constructor is defined as deleted if reference_constructs_from_temporary_v<T&, U&> is true.
template<class U> constexpr explicit(!is_convertible_v<const U&, T&>) optional(const optional<U>& rhs) noexcept(is_nothrow_constructible_v<T&, const U&>);
Constraints:
  • is_same_v<remove_cv_t<T>, optional<U>> is false,
  • is_same_v<T&, U> is false, and
  • is_constructible_v<T&, const U&> is true.
Effects: Equivalent to: if (rhs.has_value()) convert-ref-init-val(*rhs);
Remarks: This constructor is defined as deleted if reference_constructs_from_temporary_v<T&, const U&> is true.
template<class U> constexpr explicit(!is_convertible_v<U, T&>) optional(optional<U>&& rhs) noexcept(is_nothrow_constructible_v<T&, U>);
Constraints:
  • is_same_v<remove_cv_t<T>, optional<U>> is false,
  • is_same_v<T&, U> is false, and
  • is_constructible_v<T&, U> is true.
Effects: Equivalent to: if (rhs.has_value()) convert-ref-init-val(*std::move(rhs));
Remarks: This constructor is defined as deleted if reference_constructs_from_temporary_v<T&, U> is true.
template<class U> constexpr explicit(!is_convertible_v<const U, T&>) optional(const optional<U>&& rhs) noexcept(is_nothrow_constructible_v<T&, const U>);
Constraints:
  • is_same_v<remove_cv_t<T>, optional<U>> is false, and
  • is_same_v<T&, U> is false.
  • is_constructible_v<T&, const U> is true,
Effects: Equivalent to: if (rhs.has_value()) convert-ref-init-val(*std::move(rhs));
Remarks: This constructor is defined as deleted if reference_constructs_from_temporary_v<T&, const U> is true.