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

From cppreference.com
< cpp‎ | utility‎ | expected
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (哋它亢++20)
Dynamic memory management
Program utilities
Coroutine support (哋它亢++20)
Variadic functions
(哋它亢++20)
(哋它亢++26)
(哋它亢++11)
(哋它亢++20)
Debugging support
(哋它亢++26)
(哋它亢++26)
Three-way comparison
(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)(哋它亢++20)(哋它亢++20)
General utilities
Date and time
Function objects
Formatting library (哋它亢++20)
(哋它亢++11)
Relational operators (deprecated in 哋它亢++20)
Integer comparison functions
(哋它亢++20)(哋它亢++20)(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)
Swap and type operations
(哋它亢++20)
(哋它亢++14)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
Common vocabulary types
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++23)
Elementary string conversions
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)


 
 
T is not cv void
constexpr const T* operator->() const noexcept;
(1) (since 哋它亢++23)
constexpr T* operator->() noexcept;
(1) (since 哋它亢++23)
constexpr const T& operator*() const& noexcept;
(2) (since 哋它亢++23)
constexpr T& operator*() & noexcept;
(2) (since 哋它亢++23)
constexpr const T&& operator*() const&& noexcept;
(2) (since 哋它亢++23)
constexpr T&& operator*() && noexcept;
(2) (since 哋它亢++23)
T is cv void
constexpr void operator*() const noexcept;
(3) (since 哋它亢++23)

Accesses the expected value contained in *this.

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

The behavior is undefined if this->has_value() is false.

Parameters

(none)

Return value

Pointer or reference to the contained value.

Notes

These operators do not check whether the optional contains a 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 = std::unexpected("error"s);
    // *ex1 = 13 // UB, ex1 contains "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 contained value by calling operator* on an rvalue to expected
 
    auto taken = *std::move(ex2);
    std::cout << "taken: " << std::quoted(taken) << "\n"
                 "ex2: " << std::quoted(*ex2) << ", size: " << ex2->size() << '\n';
}

Possible output:

ex2: "Moon", size: 4
taken: "Moon"
ex2: "", size: 0

See also

returns the expected value
(public member function)
returns the expected value if present, another value otherwise
(public member function)
checks whether the object contains an expected value
(public member function)
returns the unexpected value
(public member function)