std::optional<T>::value
Min standard notice:
If *this contains a value, returns a reference to the contained value.
# Declarations
constexpr T& value() &;
constexpr const T& value() const &;
(since C++17)
constexpr T&& value() &&;
constexpr const T&& value() const &&;
(since C++17)
# Return value
A reference to the contained value.
# Notes
The dereference operator operator*() does not check if this optional contains a value, which may be more efficient than value().
# Example
#include <iostream>
#include <optional>
int main()
{
std::optional<int> opt = {};
try
{
[[maybe_unused]] int n = opt.value();
}
catch(const std::bad_optional_access& e)
{
std::cout << e.what() << '\n';
}
try
{
opt.value() = 42;
}
catch(const std::bad_optional_access& e)
{
std::cout << e.what() << '\n';
}
opt = 43;
std::cout << *opt << '\n';
opt.value() = 44;
std::cout << opt.value() << '\n';
}