Namespaces
Variants

std::optional<T>:: end

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

Devuelve un iterador más allá del final. Equivalente a return begin ( ) + has_value ( ) ; .

range-begin-end.svg

Contenidos

Valor de retorno

Iterador más allá del final

Complejidad

Constante.

Notas

Macro de prueba de características Valor Std Característica
__cpp_lib_optional_range_support 202406L (C++26) Soporte de rango para std::optional

Ejemplo

#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());
    // soporte para bucle for basado en rangos
    for (int i : none)
        std::println("Optional @1 has a value of {}", i);
    for (int i : some)
        std::println("Optional @2 has a value of {}", i);
}

Salida:

Optional @2 has a value of 42

Véase también

(C++26)
devuelve un iterador al inicio
(función miembro pública)