std::expected::value(3)
Hey bestie π«Ά welcome to the std::expected::value(3) page from the C++ Standard Libary manual β yes, that spelling is in the header, and honestly? Keeping it π. This is the whole tea on pulling a value out of a std::expected when the vibe check passes, plus the very specific exception situation when it doesn't π.
π NAME π
std::expected::value β std::expected::value π
β¨ Synopsis β¨
Okay so first up: when T is not (possibly cv-qualified) void, you get these overloads:
T is not (possibly cv-qualified) void
constexpr T& value() &; (1) (since C++23)
constexpr const T& value() const&; (2) (since C++23)
constexpr T&& value() &&; (3) (since C++23)
constexpr const T&& value() const&&; (4) (since C++23)
T is (possibly cv-qualified) void
constexpr void value() const&; (5) (since C++23)
constexpr void value() &&; (6) (since C++23)
If *this contains an expected value, it returns a reference to the contained value. Slay π
.
It returns nothing if T is (possibly cv-qualified) void β literally nothing, no cap.
Otherwise, it throws an exception of type std::bad_expected_access<std::decay_t<E>> that contains a copy of error(). Not the fun kind of surprise π.
Now the βprogram is ill-formedβ checkpoints, because C++ loves a prerequisite:
1,2)β ifstd::is_copy_constructible_v<E>isfalse, the program is ill-formed.3,4)β ifstd::is_copy_constructible_v<E>orstd::is_constructible_v<E, decltype(std::move(error()))>isfalse, the program is ill-formed.5)β ifstd::is_copy_constructible_v<E>isfalse, the program is ill-formed.6)β ifstd::is_move_constructible_v<E>isfalse, the program is ill-formed.
π Parameters π
(none) β bestie, there are literally no parameters. Iconic minimalism β¨.
π¦ Return value π¦
1-4)β the expected value contained in*this.5,6)β(none).
πΈ Exceptions πΈ
1,2,5)β throwsstd::bad_expected_access(std::as_const(error()))if*thiscontains an unexpected value.3,4,6)β throwsstd::bad_expected_access(std::move(error()))if*thiscontains an unexpected value.
These are the βchecked accessβ moments, so yeah β if the unexpected value is sitting there, it is not quietly letting it slide π€.
π Example π
This section is incomplete β Reason: no example π.
Not me staring at an empty example section. The audacity. Anyway, thatβs exactly what the page says, bestie.
π₯ Defect reports π₯
Heads up: the following behavior-changing defect reports were applied retroactively to previously published C++ standards. History said βnot today,β but the committee said βactually, yesβ π.
- DR:
LWG 3940β Applied to:C++23. Behavior as published:Ewas not required to be copy/move-constructible for overloads(5,6). Correct behavior: required.
π See also π
Go peek at these besties:
value_orβ returns the expected value if present, another value otherwise (public member function).operator->β accesses the expected value.operator*β(public member function).errorβ returns the unexpected value (public member function).bad_expected_access(C++23) β exception indicating checked access to an expected that contains an unexpected value (class template).
π Category π
* Todo no example
Anyway, thatβs the page, bestie π it lives at http://cppreference.com, dated 2024.06.10, and itβs std::expected::value(3) β go forth and check your expected values, mwah π«Άπβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms