Namespaces
Variants

std::optional<T>::begin

Von de.cppreference.net
 
 
Dienstprogrammbibliothek
Sprachunterstützung
Typunterstützung (Basistypen, RTTI)
Bibliotheks-Feature-Test-Makros (C++20)
Programm-Dienstprogramme
Variadische Funktionen
Koroutinenunterstü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 Dienstprogramme
Relationale Operatoren (veraltet in C++20)
Ganzzahl-Vergleichsfunktionen
(C++20)(C++20)(C++20)    
(C++20)
Tausch und Typoperationen
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Allgemeine Vokabular-Typen
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
 
constexpr iterator begin() noexcept;
(seit C++26)
constexpr const_iterator begin() const noexcept;
(seit C++26)

Falls *this einen Wert enthält, wird ein Iterator auf den enthaltenen Wert zurückgegeben. Andernfalls ein Iterator, der das Ende markiert.

Rückgabewert

Iterator zum enthaltenen Wert, falls has_value ( ) gleich true ist. Andernfalls ein Past-the-End-Iterator.

Komplexität

Konstante.

Hinweise

Feature-Test Makro Wert Std Funktion
__cpp_lib_optional_range_support 202406L (C++26) Bereichsunterstützung für std::optional

Beispiel

#include <optional>
#include <print>
#include <vector>
int main()
{
    constexpr std::optional<int> none{std::nullopt};
    constexpr std::optional<int> some{42};
    static_assert(none.begin() == none.end());
    static_assert(some.begin() != some.end());
    // Unterstützung für Range-basierte for-Schleifen
    for (int i : none)
        std::println("'none' has a value of {}", i);
    for (int i : some)
        std::println("'some' has a value of {}", i);
    std::optional<std::vector<int>> many({0, 1, 2});
    for (const auto& v : many)
        std::println("'many' has a value of {}", v);
}

Ausgabe:

'some' has a value of 42
'many' has a value of [0, 1, 2]

Siehe auch

(C++26)
gibt einen Iterator zum Ende zurück
(öffentliche Elementfunktion)