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.
The behavior is undefined if *this
does not contain a value.
(none).
Pointer or reference to the contained value.
(none).
This operator does not check whether the optional contains a value. If checked access is needed, value()
or value_or()
may be used.
#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
returns the contained value (public member function) |
|
returns the contained value if available, another value otherwise (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/utility/optional/operator_star_