std::experi...::operator*(3)

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

πŸ’– std::experimental::optional::operator->, std::experimental::optional::operator* πŸ’–

okay so we're in the C++ Standard Library manual, bestie, and this page is about std::experimental::optional's operator-> and operator*. two lil access operators, one big vibe. πŸ’…

✨ NAME ✨

std::experimental::optional::operator->, std::experimental::optional::operator* β€” yes bestie, both of them, the arrow one and the star one. no cap, this page covers the whole duo. πŸ¦‹

🌟 Synopsis 🌟

here's the overload family exactly as the page lays it out, library fundamentals TS and all:

          constexpr const T* operator->() const;   (1) (library fundamentals
       TS)
          constexpr T* operator->();               (1) (library fundamentals
       TS)
          constexpr const T& operator*() const&;   (2) (library fundamentals
       TS)
          constexpr T& operator*() &;              (2) (library fundamentals
       TS)
          constexpr const T&& operator*() const&&; (2) (library fundamentals
       TS)
          constexpr T&& operator*() &&;            (2) (library fundamentals
       TS)

both of these access the contained value, periodt. πŸ”₯

  1. returns a pointer to the contained value.
  2. returns a reference to the contained value.
⚠️ The behavior is undefined if *this does not contain a value. like, genuinely undefined, bestie. not a cute little vibe check. do not test it. 😭

πŸŽ€ Parameters πŸŽ€

none. zero. (none). it's giving "no thoughts, just access." πŸ™ˆ

🌸 Return value 🌸

a pointer or reference to the contained value. that's the whole return-value tea. 🍡

πŸ¦‹ Exceptions πŸ¦‹

throws nothing. not even one tiny exception. literally nothing. slay. ✨

πŸ’œ Notes πŸ’œ

this operator does not check whether the optional contains a value. no vibe check, no safety net, nothing. if checked access is needed, value() or value_or() may be used. iykyk. πŸ’€

🧚 Example 🧚

// Run this code

#include <experimental/optional>
#include <iostream>
#include <string>
using namespace std::literals;

int main()
{
    std::experimental::optional<int> opt1 = 1;
    std::cout << *opt1 << '\n';

    std::experimental::optional<std::string> opt2 = "abc"s;
    std::cout << opt2->size() << '\n';
}

πŸ–¨οΈ Output

1
3

ate. no crumbs. πŸ‘‘

πŸ‘€ See also πŸ‘€

  • value β€” returns the contained value (public member function)
  • value_or β€” returns the contained value if available, another value otherwise (public member function)

🏷️ Category 🏷️

  • * Noindexed pages

shoutout to http://cppreference.com, version 2024.06.10, and the page std::experi...::operator*(3) β€” that's the footer, bestie, mwah πŸ’–πŸŽ€

πŸ“¦ where this page comes from