std::optional<T>::operator->, std::optional<T>::operator*
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Relational operators (deprecated in C++20)
Integer comparison functions
Swap and type operations
Common vocabulary types
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
std::optional
optional::operator->optional::operator*
(C++26)
(C++26)
(C++23)
(C++23)
(C++23)
constexpr const T* operator->() const noexcept;
(1)
(since C++17)
constexpr T* operator->() noexcept;
(2)
(since C++17)
constexpr const T& operator*() const& noexcept;
(3)
(since C++17)
constexpr T& operator*() & noexcept;
(4)
(since C++17)
constexpr const T&& operator*() const&& noexcept;
(5)
(since C++17)
constexpr T&& operator*() && noexcept;
(6)
(since C++17)
Accesses the contained value.
1,2) Returns a pointer to the contained value.
3-6) Returns a reference to the contained value.
If has_value() is false, the behavior is undefined.
(until C++26)If has_value() is false:
- If the implementation is hardened, a contract violation occurs. Moreover, if the contract-violation handler returns under "observe" evaluation semantic, the behavior is undefined.
- If the implementation is not hardened, the behavior is undefined.
[edit] Return value
1,2)
val
3,4) *
val
5,6) std::move(*
val
)[edit] Notes
This operator does not check whether *this contains a value, users can do so manually by using has_value() or operator bool() . Alternatively, if checked access is needed, value() or value_or() may be used.
[edit] Example
Run this code
#include <iomanip> #include <iostream> #include <optional> #include <string> int main() { using namespace std::string_literals; std::optional <int> opt1{1}; std::cout << "opt1: " << *opt1 << '\n'; *opt1 = 2; std::cout << "opt1: " << *opt1 << '\n'; std::optional <std::string > opt2{"abc"s}; std::cout << "opt2: " << std::quoted (*opt2) << ", size: " << opt2->size() << '\n'; // You can "take" the contained value by calling operator* on an rvalue to optional auto taken = *std::move(opt2); std::cout << "taken: " << std::quoted (taken) << "\n" "opt2: " << std::quoted (*opt2) << ", size: " << opt2->size() << '\n'; }
Output:
opt1: 1 opt1: 2 opt2: "abc", size: 3 taken: "abc" opt2: "", size: 0
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2762 | C++17 | operator-> and operator* might be potentially-throwing
|
made noexcept |