std::optional::value - cppreference.com (original) (raw)

constexpr T& value() &; constexpr const T& value() const &; (1) (since C++17)
constexpr T&& value() &&; constexpr const T&& value() const &&; (2) (since C++17)

If *this contains a value, returns a reference to the contained value.

Otherwise, throws a std::bad_optional_access exception.

[edit] Parameters

(none)

[edit] Return value

A reference to the contained value.

[edit] Exceptions

std::bad_optional_access if *this does not contain a value.

[edit] Notes

The dereference operator operator*() does not check if this optional contains a value, which may be more efficient than value().

[edit] Example

Output:

bad optional access bad optional access 43 44

[edit] See also