std::experimental::optional<T>::operator->, std::experimental::optional<T>::operator*

Accesses the contained value.

# Declarations

constexpr const T* operator->() const;

(library fundamentals TS)

constexpr T* operator->();

(library fundamentals TS)

constexpr const T& operator*() const&;

(library fundamentals TS)

constexpr T& operator*() &;

(library fundamentals TS)

constexpr const T&& operator*() const&&;

(library fundamentals TS)

constexpr T&& operator*() &&;

(library fundamentals TS)

# Return value

Pointer or reference to the contained value.

# Notes

This operator does not check whether the optional contains a value. If checked access is needed, value() or value_or() may be used.

# Example

#include <experimental/optional>
#include <iostream>
#include <string>
using namespace std::literals;
 
int main()
{
    std::experimental::optional<int> opt1 = 1;
    std::cout << *opt1 << '\n';
 
    std::experimental::optional<std::string> opt2 = "abc"s;
    std::cout << opt2->size() << '\n';
}

# See also