std::expect...::operator*(3)
π
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 ifthis->has_value()isfalse. 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()orvalue_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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms