Namespaces
Variants
Actions

std::optional<T>::end

From cppreference.com
< cpp‎ | utility‎ | optional
 
C++
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
std::optional
constexpr iterator end() noexcept;
(since C++26)
constexpr const_iterator end() const noexcept;
(since C++26)

Returns a past-the-end iterator. Equivalent to return begin() + has_value();.

range-begin-end.svg

Contents

[edit] Return value

Past-the-end iterator

[edit] Complexity

Constant.

[edit] Notes

Feature-test macro Value Std Feature
__cpp_lib_optional_range_support 202406L (C++26) Range support for std::optional

[edit] Example

#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();
 
    / ranged-for loop support
    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);
}

Output:

Optional @2 has a value of 42

[edit] See also

(C++26)
returns an iterator to the beginning
(public member function) [edit]

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant