std::expected::value(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

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) β€” if std::is_copy_constructible_v<E> is false, the program is ill-formed.
  • 3,4) β€” if std::is_copy_constructible_v<E> or std::is_constructible_v<E, decltype(std::move(error()))> is false, the program is ill-formed.
  • 5) β€” if std::is_copy_constructible_v<E> is false, the program is ill-formed.
  • 6) β€” if std::is_move_constructible_v<E> is false, 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) β€” throws std::bad_expected_access(std::as_const(error())) if *this contains an unexpected value.
  • 3,4,6) β€” throws std::bad_expected_access(std::move(error())) if *this contains 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: E was 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