std::experi...onal::value(3)
π 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. π
- Equivalent to
return bool(*this) ? *val : throw bad_optional_access();. - 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
- 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