std::expected<T,E>::operator->, std::expected<T,E>::operator*

Accesses the expected value contained in *this.

# Declarations

# Primary template

constexpr const T* operator->() const noexcept;

(since C++23)

constexpr T* operator->() noexcept;

(since C++23)

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

(since C++23)

constexpr T& operator*() & noexcept;

(since C++23)

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

(since C++23)

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

(since C++23)

# void partial specialization

constexpr void operator*() const noexcept;

(since C++23)

# Notes

These operators do not check whether the optional represents an expected value! You can do so manually by using has_value() or simply operator bool(). Alternatively, if checked access is needed, value() or value_or() may be used.

# Example

#include <cassert>
#include <expected>
#include <iomanip>
#include <iostream>
#include <string>
 
int main()
{
    using namespace std::string_literals;
 
    std::expected<int, std::string> ex1 = 6;
    assert(*ex1 == 6);
 
    *ex1 = 9;
    assert(*ex1 == 9);
 
    // *ex1 = "error"s; // error, ex1 contains an expected value of type int
    ex1 = std::unexpected("error"s);
    // *ex1 = 13; // UB, ex1 contains an unexpected value
    assert(ex1.value_or(42) == 42);
 
    std::expected<std::string, bool> ex2 = "Moon"s;
    std::cout << "ex2: " << std::quoted(*ex2) << ", size: " << ex2->size() << '\n';
 
    // You can "take" the expected value by calling operator* on an std::expected rvalue
 
    auto taken = *std::move(ex2);
    std::cout << "taken " << std::quoted(taken) << "\n"
                 "ex2: " << std::quoted(*ex2) << ", size: " << ex2->size() << '\n';
}

# See also