std::expected::value_or(3)

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

🌸 NAME 🌸

std::expected::value_or β€” okay bestie, the name is literally std::expected::value_or, and this is the C++ Standard Libary manual page for std::expected::value_or(3) (yes, β€œLibary” is spelled like that in the source, no cap). It’s giving β€œfallback value when your expected doesn’t have the goods” energy. 🫢

✨ Synopsis ✨

template< class U >                               (1) (since C++23)
constexpr T value_or( U&& default_value ) const&;
template< class U >                               (2) (since C++23)
constexpr T value_or( U&& default_value ) &&;

Returns the contained value if *this contains an expected value, otherwise returns default_value. Slay. πŸ’…

  1. Returns bool(*this) ? **this : static_cast<T>(std::forward<U>(default_value))
  2. Returns bool(*this) ? std::move(**this) : static_cast<T>(std::forward<U>(default_value))

πŸŽ€ Parameters πŸŽ€

  • default_value β€” the value to use in case *this does not contain an expected value. That’s your fallback bestie, the one who shows up when the value doesn’t. πŸ’–

🧚 Type requirements 🧚

  • T must meet the requirements of CopyConstructible in order to use overload (1).
  • T must meet the requirements of MoveConstructible in order to use overload (2).
  • U&& must be convertible to T.

🌈 Return value 🌈

The currently contained value if *this contains an expected value, or default_value otherwise. That’s it, that’s the tea. β˜•

πŸ”₯ Exceptions πŸ”₯

Any exception thrown by the selected constructor of the return value T. So yeah, if T’s constructor decides to throw, that energy comes straight through. 😭

πŸ’… Notes πŸ’…

If T is (possibly cv-qualified) void, this member is not declared. Periodt. 🫢

πŸ’€ Example πŸ’€

This section is incomplete.

Reason: no example.

πŸ‘€ See also πŸ‘€

  • value β€” returns the expected value (public member function). lowkey the main character version of this. ✨

🏷️ Category 🏷️

  • Todo no example

made with πŸ’– by http://cppreference.com β€” 2024.06.10 πŸ’…

πŸ“¦ where this page comes from