Namensräume
Varianten
Aktionen

std::optional<T>::reset

Von cppreference.com
< cpp‎ | utility‎ | optional
 
 
Dienstprogramm-Bibliotheken
Sprachunterstützung
Typunterstützung (Basistypen, RTTI)
Bibliotheks-Feature-Test-Makros (C++20)
Programm-Dienstprogramme
Variadische Funktionen
Coroutine-Unterstützung (C++20)
Vertragsunterstützung (C++26)
Drei-Wege-Vergleich
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

Allgemeine Hilfsmittel
Relationale Operatoren (in C++20 veraltet)
 
 
void reset() noexcept;
(seit C++17)
(constexpr seit C++20)

Wenn *this einen Wert enthält, zerstöre diesen Wert, als ob durch value().T::~T(). Andernfalls hat dies keine Auswirkungen.

*this enthält nach diesem Aufruf keinen Wert.

Inhalt

[edit] Hinweise

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

[edit] 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

[edit] 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 Zerstörung in C++20 in constexpr erlaubt ist. zu constexpr gemacht

[edit] Siehe auch

weist Inhalte zu
(public member function) [edit]