This page is a snapshot from the LWG issues list, see theLibrary Active Issues List for more information and the meaning ofC++23 status.
std::expected<T,E>::value() & assumesE is copy constructibleSection: 22.8.6.6[expected.object.obs]Status:C++23Submitter: Jonathan WakelyOpened: 2022-12-20Last modified: 2023-11-22
Priority:Not Prioritized
View all issues withC++23 status.
Discussion:
22.8.6.6[expected.object.obs] p9 says:
Throws:bad_expected_access(error())ifhas_value()isfalse.
But iferror() returns a reference to a move-only typethen it can't be copied and the function body is ill-formed.Should it be constrained withis_copy_constructible_v<E>?Or just mandate it?
Similarly, thevalue()&& andvalue() const&&overloads requireis_move_constructible_v<E> to be trueforbad_expected_access(std::move(error())) to be valid.Casey Carter pointed out they also require it to be copyable so that theexception can be thrown, as per 14.2[except.throw] p5.
[Issaquah 2023-02-09; LWG]
Move to Immediate for C++23
[2023-02-13 Approved at February 2023 meeting in Issaquah. Status changed: Immediate → WP.]
Proposed resolution:
Modify 22.8.6.6[expected.object.obs] as indicated:
constexpr const T& value() const &;constexpr T& value() &;-?-Mandates:
is_copy_constructible_v<E>istrue.-8-Returns:
val, ifhas_value()istrue.-9-Throws:
bad_expected_access(as_const(error()))ifhas_value()isfalse.constexpr T&& value() &&;constexpr const T&& value() const &&;-?-Mandates:
is_copy_constructible_v<E>istrueandis_constructible_v<E, decltype(std::move(error()))>istrue.-10-Returns:
std::move(val), ifhas_value()istrue.-11-Throws:
bad_expected_access(std::move(error()))ifhas_value()isfalse.