[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/utility/optional/operator* [Back]  [Original]

std::optional<T>::operator->, std::optional<T>::operator* cppreference.com
cppreference.com

std::optional<T>::operator->, std::optional<T>::operator*

cppreference.com
 
C++
(C++20)
(C++20)
(C++11)
(C++20)
/
(C++11)
(C++11)
(C++11)
(C++17)
 
 
 
<tbody> </tbody>
constexpr const T* operator->() const noexcept;
(1) ( C++17)
constexpr T* operator->() noexcept;
(1) ( C++17)
constexpr const T& operator*() const& noexcept;
(2) ( C++17)
constexpr T& operator*() & noexcept;
(2) ( C++17)
constexpr const T&& operator*() const&& noexcept;
(2) ( C++17)
constexpr T&& operator*() && noexcept;
(2) ( C++17)

.

1) .
2) .

, *this .

()

.

, optional ! , has_value() operator bool(). , , value() value_or().

#include <optional>
#include <iostream>
#include <iomanip>
#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 = ""s;
    std::cout<< "opt2: " << std::quoted(*opt2) << " : " << opt2->size() << '\n';
    
    //   ""  ,  operator*
    //  rvalue  optional
    
    auto taken = *std::move(opt2);
    std::cout << "taken: " << std::quoted(taken) << '\n'
              << "opt2: " << std::quoted(*opt2)
              << ", : " << opt2->size()  << '\n';
}

:

opt1: 1
opt1: 2
opt2:  : 3
taken: 
opt2: "", : 0

C++:

LWG 2762 C++17 operator-> operator* noexcept


(public -) []
, ,
(public -) []

Web Proxy Viewer  |  New URL  |  Original Page