std::experi...onal::value(3)

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

πŸ’– std::experimental::optional::value πŸ’–

okay bestie, this one comes straight from the C++ Standard Library (specifically the library fundamentals TS, iykyk) and it’s the checked-access icon of std::experimental::optional. ✨

🌸 NAME 🌸

std::experimental::optional::value β€” std::experimental::optional::value

yes, the page title and the NAME line are literally twins. no cap. πŸ’…


✨ Synopsis ✨

constexpr T& value() &;               (1) (library fundamentals TS)
constexpr const T & value() const &;
constexpr T&& value() &&;             (2) (library fundamentals TS)
constexpr const T&& value() const &&;

Returns the contained value. πŸ’–

  1. Equivalent to return bool(*this) ? *val : throw bad_optional_access();.
  2. Equivalent to return bool(*this) ? std::move(*val) : throw bad_optional_access();.

πŸŽ€ Parameters πŸŽ€

(none) β€” she takes no arguments, bestie. literally none. πŸ’€

πŸ¦‹ Return value πŸ¦‹

A reference to the contained value. 🫢

🌈 Exceptions 🌈

std::experimental::bad_optional_access if *this does not contain a value. 😭

okay so if the optional is empty, value() does not play nice β€” she throws. checked access, periodt.

⭐ Notes ⭐

The dereference operator operator*() does not check if this optional contains a value, which may be more efficient than value(). πŸ‘‘

πŸ“ Example πŸ“

// Run this code

#include <experimental/optional>
#include <iostream>

int main()
{
    std::experimental::optional<int> opt = {};

    try
    {
        int n = opt.value();
    }
    catch (const std::logic_error& e)
    {
        std::cout << e.what() << '\n';
    }
}

Possible output:

optional<T>::value: not engaged

πŸ”₯ See also πŸ”₯

  • value_or β€” returns the contained value if available, another value otherwise (public member function)
  • operator->, operator* β€” accesses the contained value (public member function)
  • bad_optional_access (library fundamentals TS) β€” exception indicating checked access to an optional that doesn’t contain a value (class)

🏷️ Category 🏷️

  • Category: Noindexed pages

mwah πŸ’‹ β€” this was std::experi...onal::value(3) from http://cppreference.com, dated 2024.06.10. source/version vibes, locked in. πŸŽ€βœ¨

πŸ“¦ where this page comes from