Namespaces
Variants

std::optional<T>:: end

From cppreference.net
Utilities library
constexpr iterator end ( ) noexcept ;
(seit C++26)
constexpr const_iterator end ( ) const noexcept ;
(seit C++26)

Gibt einen Past-the-End-Iterator zurück. Entspricht return begin ( ) + has_value ( ) ; .

range-begin-end.svg

Inhaltsverzeichnis

Rückgabewert

Past-the-end-Iterator

Komplexität

Konstante.

Hinweise

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

Beispiel

#include <optional>
#include <print>
int main()
{
    constexpr std::optional<int> none{std::nullopt}; // optional @1
    constexpr std::optional<int> some{42};           // optional @2
    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("Optional @1 hat einen Wert von {}", i);
    for (int i : some)
        std::println("Optional @2 hat einen Wert von {}", i);
}

Ausgabe:

Optional @2 hat einen Wert von 42

Siehe auch

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