std::option...::operator*(3)
π NAME π
hey bestie, welcome to the std::option...::operator*(3) page from the C++ Standard Libary manual β¨. Your faves are std::optional::operator->,std::optional::operator* - std::optional::operator->,std::optional::operator*. Yes, the page really does say it twice β iconic behavior, no notes π
.
β¨ Synopsis β¨
okay so here's the whole signature situation, copied exactly:
constexpr const T* operator->() const noexcept; (1) (since C++17)
constexpr T* operator->() noexcept; (1) (since C++17)
constexpr const T& operator*() const& noexcept; (2) (since C++17)
constexpr T& operator*() & noexcept; (2) (since C++17)
constexpr const T&& operator*() const&& noexcept; (2) (since C++17)
constexpr T&& operator*() && noexcept; (2) (since C++17)
Accesses the contained value.
- Returns a pointer to the contained value.
- Returns a reference to the contained value.
β οΈ The behavior is undefined if *this does not contain a value. Like, actually undefined β do not play with that, bestie π.
π Parameters π
Absolutely none, babe: (none).
π Return value π
Pointer or reference to the contained value. That's it, that's the whole vibe π«Ά.
π§ Notes π§
π¨ This operator does not check whether the optional contains a value!
You can do so manually by using has_value() or simply operator bool(). Alternatively, if checked access is needed, value() or value_or() may be used. Slay π
.
πΈ Example πΈ
run this code, bestie:
// Run this code
#include <iomanip>
#include <iostream>
#include <optional>
#include <string>
int main()
{
using namespace std::string_literals;
std::optional<int> opt1 = 1;
std::cout << "opt1: " << *opt1 << '\n';
*opt1 = 2;
std::cout << "opt1: " << *opt1 << '\n';
std::optional<std::string> opt2 = "abc"s;
std::cout << "opt2: " << std::quoted(*opt2) << ", size: " <<
opt2->size() << '\n';
// You can "take" the contained value by calling operator* on an rvalue to optional
auto taken = *std::move(opt2);
std::cout << "taken: " << std::quoted(taken) << "\n"
"opt2: " << std::quoted(*opt2) << ", size: " <<
opt2->size() << '\n';
}
π« Output π«
opt1: 1
opt1: 2
opt2: "abc", size: 3
taken: "abc"
opt2: "", size: 0
π©Ή Defect reports π©Ή
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR:
LWG 2762 - Applied to:
C++17 - Behavior as published:
operator->andoperator*might be potentially-throwing - Correct behavior: made
noexcept
periodt π«‘.
π See also π
valueβ returns the contained value (public member function)value_orβ returns the contained value if available, another value otherwise (public member function)
mwah π cute little footer moment: source/version http://cppreference.com, date 2024.06.10, and this has been the std::option...::operator*(3) page from the C++ Standard Libary manual. bye bestie π«Άπβ¨
π¦ 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