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

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

πŸ’… std::expect...::operator*(3) β€” bestie edition

okay so this is a page from the C++ Standard Libary manual (yes that's how it's spelled on the page, don't come for me πŸ’€) and it's all about how you reach inside a std::expected and grab the thing it's actually holding. it's giving "peek behind the curtain" energy and honestly? iconic.


✨ NAME ✨

std::expected::operator->, std::expected::operator* β€” yes both of them, they're siblings, they come as a package 🫢


🌸 SYNOPSIS 🌸

first case, when T is not cv void:

constexpr const T* operator->() const noexcept;   (1) (since C++23)
constexpr T* operator->() noexcept;               (1) (since C++23)
constexpr const T& operator*() const& noexcept;   (2) (since C++23)
constexpr T& operator*() & noexcept;              (2) (since C++23)
constexpr const T&& operator*() const&& noexcept; (2) (since C++23)
constexpr T&& operator*() && noexcept;            (2) (since C++23)

and then the special case, when T is cv void:

constexpr void operator*() const noexcept;        (3) (since C++23)
all of these are since C++23 btw, so if your compiler is acting brand new and doesn't know them, that's why πŸ™ˆ

πŸ¦‹ DESCRIPTION πŸ¦‹

the whole vibe: these operators access the expected value contained in *this. that's it, that's the tweet.

  • (1) hands you back a pointer to the contained value. pointer energy πŸ”₯
  • (2) hands you back a reference to the contained value. reference energy πŸ”₯
  • (3) hands you back... nothing. literally nothing. void. "Returns nothing." 😭
⚠️ and here's the part where you gotta be careful bestie: the behavior is undefined if this->has_value() is false. not "it throws", not "it returns a default", undefined. like, main-character-walking-into-traffic undefined. do not.

πŸŽ€ PARAMETERS πŸŽ€

none. zero. not a single one. she takes no arguments ✨


πŸ’– RETURN VALUE πŸ’–

a pointer or a reference to the contained value. (or void, in case (3), because we love an empty-handed queen πŸ‘‘)


🧚 NOTES 🧚

⚠️ these operators do not check whether the "optional" contains a value! (the page says "optional" β€” we all know it means the expected, okurrr, but I'm keeping the receipts πŸ“)

if you do wanna check, you got options, bestie:

  • do it manually with has_value()
  • or just use operator bool(), simple and cute
  • alternatively, if checked access is what you need, reach for value() or value_or() instead πŸ’œ

🌈 EXAMPLE 🌈

// Run this code (their words, not mine, but like... run it)

#include <cassert>
#include <expected>
#include <iomanip>
#include <iostream>
#include <string>

int main()
{
    using namespace std::string_literals;

    std::expected<int, std::string> ex1 = 6;
    assert(*ex1 == 6);

    *ex1 = 9;
    assert(*ex1 == 9);

    ex1 = std::unexpected("error"s);
    // *ex1 = 13 // UB, ex1 contains "unexpected" value
    assert(ex1.value_or(42) == 42);

    std::expected<std::string, bool> ex2 = "Moon"s;
    std::cout << "ex2: " << std::quoted(*ex2) << ", size: " << ex2->size() << '\n';

    // You can "take" the contained value by calling operator* on an rvalue to expected

    auto taken = *std::move(ex2);
    std::cout << "taken: " << std::quoted(taken) << "\n"
                 "ex2: " << std::quoted(*ex2) << ", size: " << ex2->size() << '\n';
}

notice that *ex1 = 13 line is commented out? yeah. UB. they're showing you the danger zone, not inviting you in πŸ’€


⭐ POSSIBLE OUTPUT ⭐

ex2: "Moon", size: 4
taken: "Moon"
ex2: "", size: 0

the way ex2 gets emptied out after the std::move... she gave everything she had and now there's nothing left. respect. 🫑


πŸ‘‘ SEE ALSO πŸ‘‘

  • value β€” returns the expected value (public member function)
  • value_or β€” returns the expected value if present, another value otherwise (public member function)
  • operator bool, has_value β€” checks whether the object contains an expected value (public member function)
  • error β€” returns the unexpected value (public member function)

and that's the page!! πŸ’… this lil beauty came from http://cppreference.com, dated 2024.06.10, and it's literally page std::expect...::operator*(3). go forth and dereference responsibly, bestie β€” and pls pls pls check has_value() first πŸ™ πŸ’œβœ¨

πŸ“¦ where this page comes from