Namespaces
Variants

std::optional<T>:: reset

From cppreference.net
Utilities library
void reset ( ) noexcept ;
(seit C++17)
(constexpr seit C++20)

Wenn * this einen Wert enthält, zerstöre diesen Wert wie durch value ( ) . T :: ~T ( ) . Andernfalls gibt es keine Effekte.

* this enthält nach diesem Aufruf keinen Wert mehr.

Inhaltsverzeichnis

Hinweise

Feature-Test Makro Wert Std Feature
__cpp_lib_optional 202106L (C++20)
(DR20)
Vollständig constexpr

Beispiel

#include <iostream>
#include <optional>
struct A
{
    std::string s;
    A(std::string str) : s(std::move(str)) { std::cout << " constructed\n"; }
    ~A() { std::cout << " destructed\n"; }
    A(const A& o) : s(o.s) { std::cout << " copy constructed\n"; }
    A(A&& o) : s(std::move(o.s)) { std::cout << " move constructed\n"; }
    A& operator=(const A& other)
    {
        s = other.s;
        std::cout << " copy assigned\n";
        return *this;
    }
    A& operator=(A&& other)
    {
        s = std::move(other.s);
        std::cout << " move assigned\n";
        return *this;
    }
};
int main()
{
    std::cout << "Create empty optional:\n";
    std::optional<A> opt;
    std::cout << "Construct and assign value:\n";
    opt = A("Lorem ipsum dolor sit amet, consectetur adipiscing elit nec.");
    std::cout << "Reset optional:\n";
    opt.reset();
    std::cout << "End example\n";
}

Ausgabe:

Create empty optional:
Construct and assign value:
 constructed
 move constructed
 destructed
Reset optional:
 destructed
End example

Fehlerberichte

Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.

DR Angewendet auf Verhalten wie veröffentlicht Korrigiertes Verhalten
P2231R1 C++20 reset war nicht constexpr, während nicht-triviale Destruktion in constexpr in C++20 erlaubt ist als constexpr festgelegt

Siehe auch

weist Inhalte zu
(öffentliche Elementfunktion)