Namespaces
Variants
Views
Actions

std::experimental::optional<T>::value

From cppreference.com
 
 
Experimental
Technical Specification
Filesystem library (filesystem TS)
Library fundamentals (library fundamentals TS)
Library fundamentals 2 (library fundamentals TS v2)
Library fundamentals 3 (library fundamentals TS v3)
Extensions for parallelism (parallelism TS)
Extensions for parallelism 2 (parallelism TS v2)
Extensions for concurrency (concurrency TS)
Extensions for concurrency 2 (concurrency TS v2)
Concepts (concepts TS)
Ranges (ranges TS)
Reflection (reflection TS)
Mathematical special functions (special functions TR)
Experimental Non-TS
Pattern Matching
Linear Algebra
std::execution
Contracts
2D Graphics
 
 
 
constexpr T& value() &;
constexpr const T & value() const &;
(1) (library fundamentals TS)
constexpr T&& value() &&;
constexpr const T&& value() const &&;
(2) (library fundamentals TS)

Returns the contained value.

1) Equivalent to return bool(*this) ? *val : throw bad_optional_access();.
2) Equivalent to return bool(*this) ? std::move(*val) : throw bad_optional_access();.

Contents

[edit] Parameters

(none)

[edit] Return value

A reference to the contained value.

[edit] Exceptions

std::experimental::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

#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

[edit] See also

returns the contained value if available, another value otherwise
(public member function) [edit]
accesses the contained value
(public member function) [edit]
(library fundamentals TS)
exception indicating checked access to an optional that doesn't contain a value
(class) [edit]