std::optional<T>::end
From cppreference.com
 
 
 
 
 
 C++ 
 Feature test macros (C++20)
 Concepts library (C++20)
 Metaprogramming library (C++11)
 Ranges library (C++20)
 Filesystem library (C++17)
 Concurrency support library (C++11)
 Execution control library (C++26)
Utilities library 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
Relational operators (deprecated in C++20) Integer comparison functions  
 
 
 
 Swap and type operations  Common vocabulary types 
 Type support (basic types, RTTI)
 Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
 Coroutine support (C++20)
 Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
  (C++20)(C++20)(C++20)
  (C++20)(C++20)(C++20)
 General utilities
(C++20)(C++20)(C++20)
  (C++20)(C++20)(C++20)
(C++20)
std::optional 
 
 
 
 
 
(C++26)
optional::end
(C++26)
(C++23)
(C++23)
(C++23)
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();.
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
Run this code
#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