##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::optional::operator->, std::optional::operator*

From cppreference.com
< cpp‎ | utility‎ | optional

constexpr const T* operator->() const; | (1) | (since C++17)
constexpr T* operator->(); | (1) | (since C++17)
constexpr const T& operator*() const&; | (2) | (since C++17)
constexpr T& operator*() &; | (2) | (since C++17)
constexpr const T&& operator*() const&&; | (2) | (since C++17)
constexpr T&& operator*() &&; | (2) | (since C++17)

Accesses the contained value.

1) Returns a pointer to the contained value.
2) Returns a reference to the contained value.

The behavior is undefined if *this does not contain a value.

### [edit] Parameters

(none)

### [edit] Return value

Pointer or reference to the contained value.

### [edit] Exceptions

(none)

### [edit] Notes

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

### [edit] Example

Run this code

    #include <optional>
    #include <iostream>
    #include <string>
    int main()
    {
        using namespace std::string_literals;

        std::optional<int> opt1 = 1;
        std::cout << *opt1 << '\n';

        std::optional<std::string> opt2 = "abc"s;
        std::cout << opt2->size() << '\n';
    }

Output:

    1
    3

### [edit] See also

value | returns the contained value (public member function) [edit]
value_or | returns the contained value if available, another value otherwise (public member function) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/utility/optional/operator*&oldid=89692"